Creating matrix of given pattern
23 views (last 30 days)
Show older comments
Peter Mbamaluikem
on 3 Apr 2017
Commented: Peter Mbamaluikem
on 3 Apr 2017
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 Comments
Accepted Answer
the cyclist
on 3 Apr 2017
Inpu = zeros(11,880);
for ii = 1:11
Inpu(ii,80*(ii-1)+1:ii*80) = ones(1,80);
end
More Answers (2)
dpb
on 3 Apr 2017
Edited: dpb
on 3 Apr 2017
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...
See Also
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!