Finding the max value in a vector

Hi guys, I am having some trouble finding the highest value and its Index in a vector. Bellow is my code, I am having trouble in the line:
[V,M]=max(SUB,{},'omitnan')
The last value from "SUB"is:
Columns 1 through 7
[] [NaN] [13.6623] [-42.7241] [191.0598] [-6.5336] [-33.3556]
Columns 8 through 10
[-62.5169] [113.4669] [-24.7364]
That is why it is necessary to use 'omitnan'.
Thanks in Advance !
%importar dados do excel
imp = xlsread('Academia.xlsx');
%%Loop Kmeans K clusters
k=10
% eva = evalclusters(x,clust,criterion)
for i=1:k
[idx,C]=kmeans(imp,i);
M{i}=idx;
% y=xlswrite('Academia.xlsx',idx,'D1:D80')
eva = evalclusters(imp, idx, 'CalinskiHarabasz')
CH{i}=eva.CriterionValues
end
% % Descobrir as diferenças entre os valores de CH
while i>1
SUB{i}=CH{i}-CH{i-1}
i=i-1;
end
%Achar o pulo maximo entre os clusters para o valor de CH:
[V,M]=max(SUB,{},'omitnan')

 採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 14 日

0 投票

You do not have a numeric vector, you have a cell array. You cannot use max() on a cell array.
You could consider using cell2mat() to construct a numeric array, and calculate the maximum of that, but then you are left with the question of whether the empty SUBS{1} should be counted as taking up a space or not.
Is there a reason you are storing SUBS as a cell array and not as a numeric array initialized to infinity ?

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2016 年 2 月 14 日
編集済み: Geoff Hayes 2016 年 2 月 14 日

0 投票

Gabriel - according to max input arguments, a cell array is not a valid input to this function. Are you observing the error
Undefined function 'max' for input arguments of type 'cell'.
If this is the case, then try converting the cell array to a matrix using cell2mat as
[V,M]=max(cell2mat(SUB));
where
V =
191.0598
M =
4

5 件のコメント

Gabriel
Gabriel 2016 年 2 月 14 日
編集済み: Gabriel 2016 年 2 月 14 日
Thank you a lot, Geoff , but for some reason I am getting a diferent output: V =
218.6589
M =
3
Do u know why we get different answers?
Walter Roberson
Walter Roberson 2016 年 2 月 15 日
Your sample input that you show does not even have a 281.6589, so either you are applying max() to a different cell that you posted or else you have your own max.m that is making up answers.
Gabriel
Gabriel 2016 年 2 月 15 日
編集済み: Walter Roberson 2016 年 2 月 15 日
I think i found the reason in this part of the code:
SUB2=cell2mat(SUB)
[V,M]=max(SUB2)
SUB =
Columns 1 through 7
[] [NaN] [22.1828] [190.8772] [-33.2118] [-18.3891] [-63.9705]
Columns 8 through 10
[70.6695] [-26.9859] [-78.7988]
SUB2 =
NaN 22.1828 190.8772 -33.2118 -18.3891 -63.9705 70.6695 -26.9859 -78.7988
V =
190.8772
As you can see, when the conversion is made it ignores the first i ([]) so that the new index is 3.
Do you know how not to ignore it ?
Thanks !
M =
3
Walter Roberson
Walter Roberson 2016 年 2 月 15 日
I wrote before "You could consider using cell2mat() to construct a numeric array, and calculate the maximum of that, but then you are left with the question of whether the empty SUBS{1} should be counted as taking up a space or not." but you did not answer that.
Gabriel
Gabriel 2016 年 2 月 21 日
Sorry. Yes, it should be counted.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2016 年 2 月 14 日

コメント済み:

2016 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by