Creating matrix of given pattern
조회 수: 7 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (2개)
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 Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!