# Getting the minimum value from a row vector

1 visualizzazione (ultimi 30 giorni)
Aftab Ahmed Khan il 26 Lug 2016
Modificato: Stephen23 il 26 Lug 2016
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 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Aftab Ahmed Khan il 26 Lug 2016
Well, i am running it with a breakpoint and didn't run it up to the twelfth iteration and didn't notice the error. Thanx. On the third iteration, min_value is 3 and on the fourth iteration it picks the value 4, skipping the 2nd 3 value. Similarly for the value 6.
Aftab Ahmed Khan il 26 Lug 2016
I have made the necessary changes to the code.
a=[1,2,3,3,4,5,6,6,7,8,9,10];
min_value=0;
for i=1:length(a)
if isempty(min_value)
break;
end
min_value=min(a(a>min_value))
end

Accedi per commentare.

### Risposta accettata

Stephen23 il 26 Lug 2016
Modificato: Stephen23 il 26 Lug 2016
Why make it complicated ?
>> a = [1,2,3,3,4,5,6,6,7,8,9,10];
>> b = sort(a); % guarantees the order
>> for k = 1:numel(b), disp(b(k)), end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Stephen23 il 26 Lug 2016
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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by