Hi all,
I'm pretty stuck on this one; I want to fill a 10x100 matrix as: A =
[1 2 3 . . .100
101 102 103 .
201 202 203 .
. . .
. . .
901 . . . . 1000]
Since I want to make use of for loops, I've made the following code:
u=10;
v=100;
A=zeros(u,v);
for m=1:u;
for n=1:v;
A(m,n)= ....
end
end
I manage to get the first row going from 1 to 100, but I can't seem to get the second row (and so on) to start with A(1,1)+m*100. I can guess that the problem is in the first loop, but I just can't seem to solve it. Anyone here who can help me getting the right code for A(m,n)=....? Or am I doing it completely wrong in the first place?
Thank you!

댓글 수: 1

James Tursa
James Tursa 2017년 9월 15일
Do you have to use loops because this is a homework assignment?

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

 채택된 답변

Jan
Jan 2017년 9월 15일

0 개 추천

You are almost there: All you have to do is to set the element to 100*(m-1) + n.
Sorry for solving your homework, if it is one. Note that your teacher knows this forum also. But you have shown your effort and asked clearly.

댓글 수: 1

Kjell Star
Kjell Star 2017년 9월 16일
Thank you very much, I see why I could not think of the right answer in the first place now. Hopefully this will help me in future problems.

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

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2017년 9월 15일
편집: Andrei Bobrov 2017년 9월 15일

0 개 추천

out = (1:100) + (0:100:900)'
for older version of MATLAB:
out = bsxfun(@plus,1:100,(0:100:900)')

댓글 수: 1

Jan
Jan 2017년 9월 15일
편집: Jan 2017년 9월 16일
Note: See this example https://www.mathworks.com/matlabcentral/answers/356103-euclidean-distance-ed-calculation-in-matlab#answer_281678: The function is 10% faster with bsxfun compared to auto-expansion in R2016b. The auto-expansion is nice, but I have the impression, that the implementation is much less efficient than bsxfun.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 9월 15일

편집:

Jan
2017년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by