Using a for loop to extract values from a matrix
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello!
I have a 5x12 matrix. The purpose of the matrix is to show different temperatures during every month of a year in 5 different cities
A = [ 1 2 3 4 5 6 7 8 9 10 11 12 ; 1 2 3 4 5 6 7 8 9 10 11 12 ; 1 2 3 ... an so on]
Now what I want to do is "seperate" temperatures that are under a certain degree, for example if the temperature drops under 4 degrees celsius i want this to be displayed in the command window.
I should showcase temperatures of 1 2 3 for all the 5 cities.
I tried something like this:
for i = A(1:end)
if i < 4
disp('To cold')
end
end
I do not resive any error messages, but nothing happens, at all. It's obvious my MatLab skills are non existant so I'd really appriciate the help!!
0 Commenti
Risposte (1)
Cris LaPierre
il 7 Mag 2021
Typically this is done using your loop counter to index into your vector.
You can learn more about indexing in Ch 5 of MATLAB Onramp. Of course, you will need to set up your for loop correctly so that the loop counter is meaningful. See Ch 13 for that.
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements 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!