For loop for matrix with increasing numbers

I'm trying to use a for loop to create a 7x8 matrix that increases by 4 for each number (starting at 1).
So far I have something that goes
for
k = 1:8
a(k)=4*k;
end
just to create the first row... how would I go about adding more rows that keep adding increasing elements?

 채택된 답변

Image Analyst
Image Analyst 2016년 3월 7일

0 개 추천

Try this:
theNumber = 1;
for col = 1 : 8
for row = 1 : 7
a(row, col) = theNumber;
theNumber = theNumber + 4;
end
end

댓글 수: 1

laura9510
laura9510 2016년 3월 7일
편집: laura9510 2016년 3월 7일
I was going for increasing in the rows so I just switched the row/column. i.e.
theNumber = 1;
for row = 1:7
for col = 1:8
a(row, col) = theNumber;
theNumber = theNumber + 4;
end
end
Thanks for your help!! :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2016년 3월 7일

편집:

2016년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by