필터 지우기
필터 지우기

Creating matrix of given pattern

조회 수: 7 (최근 30일)
Peter Mbamaluikem
Peter Mbamaluikem 2017년 4월 3일
댓글: Peter Mbamaluikem 2017년 4월 3일
I want to write a matlab code that will create 11x880 matrix. the first 80 columns of row one will have 1's and the rest zeros. the second row will have its own 1's from column 81 to column 160 and the third row will be from 161 to next 80 and so.
Inpu = zeros(11,880);
Inpu(1, (1:80)) = ones(1,1:80) %this will write into the first 80
Inpu(2,(81:160)) = ones(1,1:80) % will write in the second row starting from 81 to 160
What command will be able to iterate it up to the 11th row which will have its 1's between column 801 to 880. Thanks

채택된 답변

the cyclist
the cyclist 2017년 4월 3일
Inpu = zeros(11,880);
for ii = 1:11
Inpu(ii,80*(ii-1)+1:ii*80) = ones(1,80);
end
  댓글 수: 1
Peter Mbamaluikem
Peter Mbamaluikem 2017년 4월 3일
Am very grateful, God bless you.

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

추가 답변 (2개)

Matt J
Matt J 2017년 4월 3일
편집: Matt J 2017년 4월 3일
Inpu=kron(eye(11),ones(1,80));

dpb
dpb 2017년 4월 3일
편집: dpb 2017년 4월 3일
For a much smaller size array so can see the results; size is actually immaterial to logic--
>> N=2; M=3; % pick sizes
>> A=zeros(M,M*N); % make overall match those as wanted
>> O=ones(1,N); % make the ones vector
>> j=1; % initial column position
for i=1:M % for the number rows
A(i,j:j+N-1)=O; % set the locations desirec
j=j+N; % next first column
end
>> A % see result is what wanted...
A =
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
>>
Alternatively with a slightly different starting position--
>> A=zeros(M,M*N); % make overall match those as wanted
>> O=ones(1,N); % make the ones vector
>> A(:,1:N)=repmat(O,M,1); % load first N columns every row
>> for i=2:M % move rows after first to right
A(i,:)=circshift(A(i,:),2*(i-1),2);
end
>> A
A =
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
>>
Sometimes a loop is just by far the simplest way...

카테고리

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