find values in an cell array

6 ビュー (過去 30 日間)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2020 年 5 月 8 日
Hi all,
I have an cell array that is like the one in the screenshot (see attached file).
I would like to find in each row, how many times value 1,2,3..etc exists
Could you help me with that?
thanks in advance!
Nikolas
  2 件のコメント
KSSV
KSSV 2020 年 5 月 8 日
Read about ismember
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2020 年 5 月 8 日
thanks for the answer,
just wondering if there was a quicker way
anyway thanks again!!

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

採用された回答

Walter Roberson
Walter Roberson 2020 年 5 月 8 日
cell_rows = arrayfun(@(ROW) horzcat(YourCell{ROW,:}), 1:size(YourCell,1), 'uniform', 0);
all_values = horzcat(cell_rows{:});
[G, uvals] = findgroups(all_values);
num_vals = length(uvals);
row_lens = cellfun(@length, cell_rows);
G_by_row = mat2cell(G, 1, row_lens);
counts = cellfun(@(R) [uvals(:), accumarray(R(:), 1, [num_vals 1])], G_by_row, 'uniform', 0);
The result will be a cell array with 63 entries. Each entry will be an N x 2 table, where N is the number of unique values over the entire matrix (not the number of unique for the individual row.) The first column will be the list of unique values; this will be the same for all of the arrays. The second column will be the counts for the corresponding values.
This will work even if the values stored in the array are not positive integers. It will even work if the values are not integers; however in the way it is written, it will consider two values to be different if they differ in even a single bit (so if the values were calculated and there might be round-off, there could be a problem.)
If you know that the values are all positive integers in the range 1 to something (such as 50) then the code can be simplified.
  1 件のコメント
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2020 年 5 月 8 日
thanks a lo!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by