find how many times sequences are present in a string array

3 views (last 30 days)
Hi! I have
S={1;[1 631];[1 631 618];[1 631 618 574];[1 631 618 574 608];631;[631 618];[631 618 574]}
and
T={'1' '596' '674' '' '' '' '' '';'674' '631' '1' '631' '1' '618' '631' '618';'641' '617' '674' '631' '654' '629' '625' '673';'674' '673' '674' '673' '674' '618' '631' '1';'674' '618' '1' '618' '631' '627' '631' '';'631' '1' '631' '674' '740' '' '' '';'739' '740' '733' '674' '631' '618' '631' '618';'674' '673' '674' '1' '641' '' '' '';'618' '1' '631' '618' '631' '618' '631' '618';'674' '631' '618' '631' '618' '631' '681' '675';'674' '631' '1' '631' '625' '618' '631' '';'641' '642' '618' '631' '618' '631' '627' '631';'618' '631' '1' '631' '1' '618' '631' '674';'674' '673' '674' '631' '674' '' '' '';'674' '719' '618' '631' '618' '631' '618' '631';'674' '631' '618' '631' '1' '618' '631' '627';'674' '673' '674' '673' '674' '631' '618' '1';'674' '631' '618' '631' '674' '' '' '';'674' '631' '618' '631' '618' '631' '618' '631';'674' '618' '631' '616' '545' '1442' '1595' '';'1' '1595' '1' '1583' '550' '674' '' '';'674' '631' '1' '631' '618' '' '' '';'674' '631' '618' '631' '' '' '' '';'1' '631' '618' '574' '608' '1' '608' '618';'626' '631' '1' '631' '618' '' '' '';'674' '673' '631' '618' '631' '659' '633' '631'}
and I want to know how many times the sequences in S are present in T.
E.g.
  • First sequence is 1 --> I search it in the first column of T (present three times)
  • Second sequence [1 631] ---> I search 1 in first column of T e 631 in the second columns: they have to be present contemporary (they are present contemporary for 1 time)
  • Third sequence [1 631 618] --> search 1 in first column of T, 631 in the second columns and 618 in the third columns: they have to be present contemporary (they are present contemporary for 1 time) and so on for all the others values
I want like output the sequences and the number of times that they are present in T, can you help me?
  5 Comments
pamela sulis
pamela sulis on 13 Apr 2016
Edited: pamela sulis on 13 Apr 2016
I try to explain better:
first sequence is 1 --> I search it in the first column of T (present three times) Second sequence [1 631] ---> I search 1 in first column of T e 631 in the second columns: they have to be present contemporary (they are present contemporary for 1 time) Third sequence [1 631 618] --> search 1 in first column of T, 631 in the second columns and 618 in the third columns: they have to be present contemporary (they are present contemporary for 1 time) and so on for all the others values
Guillaume
Guillaume on 13 Apr 2016
Is that correct that T is made up of strings whereas S is made up of numbers? In that case, can we assume that the numbers are always integer?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 13 Apr 2016
Edited: Guillaume on 13 Apr 2016
Here is one way to do it:
First, convert T into a matrix of number with str2double. The empty strings will be converted to NaNs, I assume NaN is never present in S anyway:
TT = str2double(T);
You can then iterate over the rows of S and use ismember or bsxfun(@eq...) to check for equality, only using the required numbers of columns from TT:
count = cellfun(@(s) sum(all(bsxfun(@eq, s, TT(:, 1:numel(s))), 2)), S)
note: in actually code, please use better variable names than S, T and TT.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!