Create a matrix of this type?

조회 수: 1 (최근 30일)
P K
P K 2019년 1월 19일
댓글: P K 2019년 1월 21일
Hello,
I want to make a matrix this type
1 0 0 0 0 0
2 0 0 0 0 0
3 4 0 0 0 0
5 6 0 0 0 0
7 8 9 0 0 0
10 11 12 0 0 0
13 14 15 16 0 0
17 18 19 20 0 0
% Alternate rows have same number of element
%Each element of the matrix is 1 larger than previous one
How to achieve it ?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 1월 20일
m = 8;
n = 6;
P = kron(triu(ones(n,m/2)),[1,1]);
P(P>0) = 1:nnz(P);
out = P';
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2019년 1월 21일
편집: Andrei Bobrov 2019년 1월 21일
m = 6;
n = 6;
lo = triu(~rem((1:n)' + (1:m),2));
out = int64(lo);
out(lo) = 1:nnz(lo);
out = out';
with kron
m = 6;
n = 6;
out = triu(kron(ones(ceil(n/2),ceil(m/2)),[1,0;0,1]));
out = out(1:n,1:m);
out(out~=0) = 1:nnz(out);
out = out';
P K
P K 2019년 1월 21일
Sir Andrei Bobrov, it took me "SO LONG" to do it and you have posted two ways to achieve it. Thank you.

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 20일
편집: madhan ravi 2019년 1월 20일
n=6; % number of elements in a row
B=mat2cell((1:20).',repelem(1:4,2));
B=cellfun( @transpose,B,'un',0);
R=cellfun( @(x) [x zeros(1,6-numel(x))],B,'un',0);
vertcat(R{:})
  댓글 수: 1
P K
P K 2019년 1월 20일
Thanks Madhan ravi. It works.Andrei Bobrov answer is more general.

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by