filling a matrix with a loop

조회 수: 1 (최근 30일)
bus14
bus14 2019년 5월 14일
편집: Stephen23 2019년 5월 16일
Hi community,
I want to create a large matrix 400x400, In this matrix I want it to have the form
A= [1 1 0 1 000000000000000000........;0 1 1 0 1 00000000000000......; 0 0 1 1 0 1 00000000000000000] and so on till it is a 400x400 matrix.
I could not find a way yet to easiliy do this. As doing this manually is way too much work.
Does anyone know how to do this?
  댓글 수: 2
Adam Danz
Adam Danz 2019년 5월 14일
편집: Adam Danz 2019년 5월 14일
What's the pattern? Are you just shifting [1101] down by 1 index to the right and padding the rest of the array with 0s?
bus14
bus14 2019년 5월 15일
Yes exactely this

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

채택된 답변

Stephen23
Stephen23 2019년 5월 14일
편집: Stephen23 2019년 5월 14일
"Does anyone know how to do this?"
This is simple and efficient with toeplitz:
>> C = [1,zeros(1,399)];
>> R = [1,1,0,1,zeros(1,396)];
>> M = toeplitz(C,R);
>> size(M)
ans =
400 400
>> M(1:9,1:20) % first rows and columns
ans =
1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0
>> M(392:400,381:400) % last rows and columns
ans =
0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
  댓글 수: 4
bus14
bus14 2019년 5월 16일
This worked perfectly for my described situation. However, I found out that I was using the wrong matrix..
Should be a 800 x 400 matrix which looks like
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
so like these two 2 colums which repeat itself every time.
Tried fixing it with Toeplitz again but toeplitz cannot handle 2 input columns.
would there be some sort of other matrix creating function that can do this?
Stephen23
Stephen23 2019년 5월 16일
편집: Stephen23 2019년 5월 16일
C = {[1,0;1,1;0,1;1,0]};
M = blkdiag(C{ones(1,200)});
Checking:
>> size(M)
ans =
800 400
>> M(1:12,1:16)
ans =
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

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

추가 답변 (3개)

Alex Mcaulley
Alex Mcaulley 2019년 5월 14일
A = repmat([1 1 0 1 zeros(1,396)],400,1);
A = cell2mat(arrayfun(@(i) circshift(A(i,:),i-1) , 1:size(A,1), 'UniformOutput',false)');

Jos (10584)
Jos (10584) 2019년 5월 14일
% clever indexing trick
A= [1 1 0 1]
N = 10 ; % smaller example! 400 in your case
X = triu(toeplitz(1:N)) ;
X(X > numel(A)) = 0 ;
tf = X > 0 ;
X(tf) = A(X(tf))

Andrei Bobrov
Andrei Bobrov 2019년 5월 15일
편집: Andrei Bobrov 2019년 5월 15일
out = full(spdiags(ones(400,3),[0,1,3],400,400));

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by