Hi, I want to find a min value that is not 0 from a cell array that has different number of elements in it:
cost = [1*11 double]; [1*12 double]; [1*7 double] and so on
at the moment I am finding the min like this:
[M,I] = min([cost_hist{:}]);
and I tried different variations with ~= 0 with no success. The index is also important.
Thanks in advance, Asher

댓글 수: 5

harjeet singh
harjeet singh 2015년 12월 28일
min means the average of every mat?
For this example, what is the expected result?
a={[0 10 0 2 4] [4 3 8] [0 5 -2 15] [2 6]}
Asher Metzger
Asher Metzger 2015년 12월 28일
the minimum number. sorry if I wasn't clear
Asher Metzger
Asher Metzger 2015년 12월 28일
and if there is a simple way I would also like to find the cell index the minimum number is in
Asher Metzger
Asher Metzger 2015년 12월 28일
in you example I would like to get -2 for the minimum and 3 for the cell number

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

 채택된 답변

Jan
Jan 2015년 12월 28일
편집: Jan 2015년 12월 28일

0 개 추천

C = {[0 10 0 2 4] [4 3 8] [0 5 -2 15] [2 6]};
minValue = Inf;
minIndex = 0;
for k = 1:numel(C)
Elements = C{k}(C{k} ~= 0);
aMin = min(Elements);
if aMin < minValue
minValue = aMin;
minIndex = k;
end
end
disp(minValue)
disp(minIndex)

추가 답변 (1개)

harjeet singh
harjeet singh 2015년 12월 28일

0 개 추천

you may try this
r=0;
c=0;
min_num=inf;
for i=1:size(cost,1)
for j=1:size(cost,2)
a=cost{i,j};
if(min(a(:))<min_num)
min_num=min(a(:));
r=i;
c=j;
end
end
end
display(strcat('position r:',num2str(r),'position c:',num2str(c)));
display(strcat('min number:',num2str(min_num)))

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2015년 12월 28일

댓글:

2015년 12월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by