Create a loop which fills numbers 1-7 into matrix until matrix length reached

조회 수: 10 (최근 30일)
This is gonna be an easy fix I'm sure. I have a matrix(21,4), and in colum 2 I would like to insert the numbers 1-7 so that they repeat until the end of the column (which is 21 rows long). So I probably need a loop of some sort I'm guessing.
Tried this, which obviously doesn't work:
for rowNumber = 1:21
for oneToSeven = 1:7
imageinformation(rowNumber, 2) = oneToSeven;
end
end

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 11월 17일
A - your matrix with size <21 x 4 >
A(:,2) = rem((0:size(A,1)-1)',7)+1;

추가 답변 (1개)

Matt Fig
Matt Fig 2012년 11월 18일
Andrei has shown you a preferred method. However, in answer to your original question, here is one way to solve it with a double loop (as you were attempting to do):
A = zeros(21,4); % Starting matrix.
for ii = 1:3
for jj = 1:7
A((ii-1)*7 + jj,2) = jj;
end
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by