How can I find all the minimum values in table using a for-loop (which has Ties)?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alfred Ofosu
il 25 Ott 2018
Commentato: Alfred Ofosu
il 25 Ott 2018
Month = [01 02 03 04 05 06]';
Price = [99 58 94 58 87 91]';
Count = [10 30 20 27 05 04]';
T = table(Month, Price, Count)
m = 1
for i = 2:size(T,1)
if (T.Price(i) <= T.Price(m))
m = i;
end
end
T(m,'Month')
I have this code that finds the minimum price and tells me which month has the minimum price. However, the answer I am getting is only the 4th month, whereas there is a minimum in the 2nd month as well. How can I get this code to show me all months with the minimum price? How can I store the answer in a vertical vector? Thanks in advance!
0 Commenti
Risposta accettata
madhan ravi
il 25 Ott 2018
Modificato: madhan ravi
il 25 Ott 2018
Month = [01 02 03 04 05 06]';
Price = [99 58 94 58 87 91]';
Count = [10 30 20 27 05 04]';
A = Price
ind = 1;
minval = A(ind);
for i =1:numel(A)
if A(i) < minval
minval = A(i);
ind = i;
elseif A(i) == minval
ind = [ind i]; % add current index
end
end
Months_with_less_price = Month(ind)
9 Commenti
Più risposte (1)
Kevin Chng
il 25 Ott 2018
Month = [01 02 03 04 05 06]';
Price = [99 58 94 58 87 91]';
[minimun,idx]= min(Price);
MinMonth=Month(Price==minimun);
%minimun is your minimun value, % MinMonth is the month having minimun value
4 Commenti
Kevin Chng
il 25 Ott 2018
which indexing is the minimun number in the vector, however, it has limitation, which is only return one of the index of your minimum number.
In your case, it will return 2. you may ignore it and change to
minimun= min(Price);
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!