Using a for loop to extract values from a matrix

조회 수: 5 (최근 30일)
Lovisa Norlin
Lovisa Norlin 2021년 5월 7일
댓글: Lovisa Norlin 2021년 5월 7일
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!!

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 5월 7일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by