フィルターのクリア

How to take number between two nested cell arrays

1 回表示 (過去 30 日間)
Ahmad Bayhaqi
Ahmad Bayhaqi 2021 年 7 月 6 日
コメント済み: Chunru 2021 年 7 月 6 日
Hi,
I have two nested cell arrays
A = {[27,28,30,31]},{[26,25,30]},{[33,29,31,27,28]};
B = {[30,64,72,85]},{[15,33,62]},{[45,62,77,84,90]};
How can I take a max value of all entry nested in A and pick a value in B which corresponds to the max value of A?
so the result will be:
C = 33
D = 45
Thank you

回答 (2 件)

KSSV
KSSV 2021 年 7 月 6 日
A = {[27,28,30,31],[26,25,30],[33,29,31,27,28]};
B = {[30,64,72,85],[15,33,62],[45,62,77,84,90]};
A = cell2mat(A) ;
B = cell2mat(B) ;
[val,idx] = max(A) ;
iwant = B(idx)
  2 件のコメント
Ahmad Bayhaqi
Ahmad Bayhaqi 2021 年 7 月 6 日
I am afraid that cell2mat doesnt work for cell.
because in A and B, I have three nested cell
A = {[27,28,30,31]},{[26,25,30]},{[33,29,31,27,28]};
B = {[30,64,72,85]},{[15,33,62]},{[45,62,77,84,90]};
so, I got this error below;
"cell2mat does not support cell arrays containing cell arrays or objects".
KSSV
KSSV 2021 年 7 月 6 日
Wy you want to put extra cell braces in there?

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


Chunru
Chunru 2021 年 7 月 6 日
You can do the following (the code you gave above is NOT a nested cell):
A = {{[27,28,30,31]},{[26,25,30]},{[33,29,31,27,28]}};
B = {{[30,64,72,85]},{[15,33,62]},{[45,62,77,84,90]}};
A1= []; B1=[];
for i=1:length(A)
A1 = [A1 A{i}{1}];
B1 = [B1 B{i}{1}];
end
[C, id] = max(A1)
C = 33
id = 8
D = B1(id)
D = 45
  6 件のコメント
Ahmad Bayhaqi
Ahmad Bayhaqi 2021 年 7 月 6 日
I am sorry for being too much asking, but still got an error message when use that line of code.
why it's said
"Cell contents reference from a non-cell array object"
Chunru
Chunru 2021 年 7 月 6 日
Your data is in a mixed format. Some elements are cells and some are arrays. Some are row vector and some are column vector. This cause the problem.
The following take care of these problems:
load('A', 'A');
load('B', 'B');
A1= []; B1=[];
for i=1:length(A)
if iscell(A{i})
A1 = [A1; A{i}{1}(:)];
B1 = [B1; B{i}{1}(:)];
else
A1 = [A1; A{i}(:)];
B1 = [B1; B{i}(:)];
end
end
[C, id] = max(A1)
D = B1(id)

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by