find how many times sequences are present in a string array

조회 수: 3 (최근 30일)
pamela sulis
pamela sulis 2016년 4월 13일
편집: Guillaume 2016년 4월 13일
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
pamela sulis
pamela sulis 2016년 4월 13일
편집: pamela sulis 2016년 4월 13일
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 2016년 4월 13일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 4월 13일
편집: Guillaume 2016년 4월 13일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by