given a large array of words and little cell how can i find for each word in the array the number of occurrences in the cell

2 ビュー (過去 30 日間)
given alrage single array or cell :
A=['apple','bad','boring','car','champ','corn'...] /* about 4585 words no repetitions */
given a single cell :
b=['apple','champ','corn','apple','corn']
how can i get for each word in A how many it occurs in b /* in matlab*/
the result should be :
res=[2,0,0,0,1,2...]
witout using loops , loops in matlab take so much time .
your answers is super appreciated , thanks in advance .

採用された回答

Benjamin Kraus
Benjamin Kraus 2017 年 12 月 26 日
Check out ismember.
Also, double-check your syntax for specifying a list of words. You need a cell-array of character vectors (using {}), or (starting in R2016b) a string array (using ""). If you copy/paste the code from your question into MATLAB, you will get a single concatenated character vector, which is probably not your intention.
  3 件のコメント
Benjamin Kraus
Benjamin Kraus 2017 年 12 月 27 日
I noticed you revised your question, and separated the commands into command blocks. If you copy and paste that code into MATLAB, you will notice that it concatenates the strings instead of creates a list. Compare the output of the following three commands in MATLAB:
b = ['apple','champ','corn','apple','corn']
b2 = {'apple','champ','corn','apple','corn'}
b3 = ["apple","champ","corn","apple","corn"] % R2016b and newer
You will notice that 'b' is a single concatenated version of the strings. I do not think that is your intention.
Regarding ismember. If you call ismember, and provide both A and b:
[Lia,Locb] = ismember(A,b);
The first output will be a list of true or false for whether the elements of A are present in b. This tells you whether they are present, but not how many occurrences of each element.
If you call ismember and reverse the arguments:
[Lib,Loca] = ismember(b,A);
Assuming all the words in b are present in A, The first output, Lib, will be all true. The second output, Loca, gives you the index into A for each element of b. Then you can use another command, like accumarray to count how often each index occurs.
res = accumarray(Loca', 1, [numel(A) 1])';
daniel zayed
daniel zayed 2017 年 12 月 28 日
thx you , first it didnt work for me becuase the 2 ' . when i have earsed it it worked for me nice . res = accumarray(Loca, 1, [numel(A) 1]);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by