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!