필터 지우기
필터 지우기

How does the matlab for loop work on a column vs. row vector of strings?

조회 수: 20 (최근 30일)
Frederick Breidt
Frederick Breidt 2022년 11월 2일
댓글: Frederick Breidt 2022년 11월 2일
displaying a vector of strings using a for loop gives different results with a column vector vs. a row vector, why is that?
words = ["this"; "is"; "text"]
words = 3×1 string array
"this" "is" "text"
for word = words
disp(word);
end
"this" "is" "text"
for word = words'
disp(word);
end
this is text

답변 (1개)

Voss
Voss 2022년 11월 2일
for iterates over columns, so if you give it a column vector, it iterates one time, with the loop variable taking the value of the whole column.
for col = eye(3)
disp(col)
end
1 0 0 0 1 0 0 0 1
Why? Because the powers that be decided that's how it should work.
From the documentation:
  • To iterate over the values of a single column vector, first transpose it to create a row vector.
  댓글 수: 1
Frederick Breidt
Frederick Breidt 2022년 11월 2일
Thank you for the answer. I had read the for loop info incorrectly, not realizing it iterated with whole columns instead of iterating through the column. That could actually be useful!

댓글을 달려면 로그인하십시오.

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by