Getting the minimum value from a row vector
이전 댓글 표시
Hi, I am calculating a min value in a row vector one after the other. There are two repetitions in the row vector. I want to pick 3 and 6 twice. The way i am doing is like this but it doesn't take care of the repetition in the vector
a=[1,2,3,3,4,5,6,6,7,8,9,10];
min_value=0;
for i=1:length(a)
min_value=min(a(a>min_value));
end
댓글 수: 5
Adam
2016년 7월 26일
What exactly is the result you are looking for? Your code currently gives an error when I paste it into Matlab so I can't even see what your current code is giving.
Aftab Ahmed Khan
2016년 7월 26일
@Aftab Ahmed Khan: your code:
a = [1,2,3,3,4,5,6,6,7,8,9,10];
min_value = 0;
for k = 1:length(a)
min_value = min(a(a>min_value));
end
Give this error:
Error using >
Matrix dimensions must agree.
Error in Untitled1 (line 6)
min_value = min(a(a>min_value));
because on twelfth iteration min_value is empty and so the comparison tries to compare an empty array with a 1x12 array. Thus the error.
"I want that the min function somehow should return the 3 and 6 twice."
Your code does not "return" or store any values, so it is not clear what you want.
Aftab Ahmed Khan
2016년 7월 26일
Aftab Ahmed Khan
2016년 7월 26일
채택된 답변
추가 답변 (1개)
Stephen23
2016년 7월 26일
a = [1,2,3,3,4,5,6,6,7,8,9,10];
f = find([1,diff(a),1]);
x = f(2:end)-f(1:end-1);
c = mat2cell(a,1,x);
where
>> c{:}
ans =
1
ans =
2
ans =
3 3
ans =
4
ans =
5
ans =
6 6
ans =
7
ans =
8
ans =
9
ans =
10
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!