Finding the max value in a vector

조회 수: 2 (최근 30일)
Gabriel
Gabriel 2016년 2월 14일
댓글: Gabriel 2016년 2월 21일
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일
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일
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by