How to find the max of a cell of a cell array

231 ビュー (過去 30 日間)
liu James
liu James 2016 年 12 月 15 日
コメント済み: Image Analyst 2016 年 12 月 15 日
I'm having issues finding the max of TR4. It's stating that max is undefined.
for s=1:34
TR1{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,4}{1,s}(2:end)));
TR2{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,5}{1,s}(1:end-1)));
TR3{s}=abs((prices{1,5}{1,s}(1:end-1))-(prices{1,4}{1,s}(2:end)));
TR4{s}={TR1(1,s) TR2(1,s) TR3(1,s)};
TR{s}=cellfun(@max,TR4(1,s))
end
  2 件のコメント
liu James
liu James 2016 年 12 月 15 日
All I'm trying to do is extract the max number between TR1, TR2, and TR3 and create TR
liu James
liu James 2016 年 12 月 15 日
I tried this
for s=1:34
TR1{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,4}{1,s}(2:end)));
TR2{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,5}{1,s}(1:end-1)));
TR3{s}=abs((prices{1,5}{1,s}(1:end-1))-(prices{1,4}{1,s}(2:end)));
TR{s}= max(TR1{s},TR2{s},TR3{s})
end
then I got this error.
Error using max
MAX with two matrices to compare and a working dimension is not supported.
Error in setParam (line 74)
TR{s}= max(TR1{s},TR2{s},TR3{s})

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

回答 (3 件)

John BG
John BG 2016 年 12 月 15 日
if all cells have same base type, you can use the following
T1={1 2 3 4}
cell2mat(T1)
max(cell2mat(T1))
ans =
4
something a bit more complicated would be to build a function like find() for cells because cells may have custom indices, the find_for_cells should map the cell first.
Do you only want to find max value or also find the matrix coordinates of max value?
If you supply the cell prices or a fragment of it I will have a look whether it would be easy to build the find_cell() function.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  2 件のコメント
liu James
liu James 2016 年 12 月 15 日
I believe the correct statement would be I'm trying to extract the list of Mex values for each row of T1, T2, T3.
for s=1:34
TR1{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,4}{1,s}(2:end)));
TR2{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,5}{1,s}(1:end-1)));
TR3{s}=abs((prices{1,5}{1,s}(1:end-1))-(prices{1,4}{1,s}(2:end)));
TR4{s}={TR1(1,s) TR2(1,s) TR3(1,s)};
TR{s}=cellfun((@max),TR4(1,s))
end
The error that I get is this.
Error using cellfun Undefined function 'max' for input arguments of type 'cell'.
Error using cellfun
Undefined function 'max' for input arguments of type 'cell'.
Error in setParam (line 75)
TR{s}=cellfun((@max),TR4(1,s))
liu James
liu James 2016 年 12 月 15 日
Can you provide an example on how to use the find function to extract the max number for each row?

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


Walter Roberson
Walter Roberson 2016 年 12 月 15 日
The result is not well defined. You have a cell array of cell arrays each of which has an vector in it. Which maximum are you looking for?
Have you considered
TR4{s} = [TR1(1,s) TR2(1,s) TR3(1,s)];
  2 件のコメント
liu James
liu James 2016 年 12 月 15 日
I'm trying to find the max out of the three vectors for all 34 sets. If that makes sense.
Let's say:
TR4{1} = [TR1(1,1) TR2(1,1) TR3(1,1)];
=[20 25. 19]
the max of TR4(1) = 25
Walter Roberson
Walter Roberson 2016 年 12 月 15 日
Do you want the max of each corresponding position? If so then
TR4{s} = [TR1{1,s}; TR2{1,s}; TR3{1,s}];
TR{s} = max(TR4{s});

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


Image Analyst
Image Analyst 2016 年 12 月 15 日
Please show the entire error message -- ALL the red text, not just a small part of it. I don't know why your max function would no longer be defined. What does this say if you type it on the command line:
>which -all max
What does it show?
  2 件のコメント
liu James
liu James 2016 年 12 月 15 日
Error using cellfun
Undefined function 'max' for input arguments of type 'cell'.
Error in setParam (line 75)
TR{s}=cellfun((@max),TR4(1,s))
Image Analyst
Image Analyst 2016 年 12 月 15 日
Try putting brackets around the numbers to make a numerical vector, and then assigning it to another numerical vector. There is certainly no need to store the maxima in a cell array - that's an unnecessary complication.
theMaxes(s) = max([TR1{s},TR2{s},TR3{s}])

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by