modify the matrix, rearrangement of elements

조회 수: 1 (최근 30일)
CHANDRABHAN Singh
CHANDRABHAN Singh 2020년 5월 23일
댓글: CHANDRABHAN Singh 2020년 5월 28일
I have a matrix a = [2 4 7 11; 7 9 5 54; 2 5 7 9; 12 41 45 21];
How to get the row matrix such that the elements of matrix a are arranged digonally upwards from the left .
For exapmle the row matrix should me [2 7 4 2 9 7 12 5 7 11 41 7 54 45 9 21];
Thanks in advance.
  댓글 수: 2
Stephen23
Stephen23 2020년 5월 27일
편집: Stephen23 2020년 5월 28일
a =
2 4 7 11
7 9 5 54
2 5 7 9
12 41 45 21
The main anti-diagonal has values [12,5,5,11] (from bottom left to top right), but your example output shows the values [...,12,5,7,11,...]: please clarify the correct expected values.
CHANDRABHAN Singh
CHANDRABHAN Singh 2020년 5월 28일
sir, it is ...12, 5, 5,11..... It is a typing mistake. Sorry for this.

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

채택된 답변

Srivardhan Gadila
Srivardhan Gadila 2020년 5월 27일
The following code might help you:
sz = size(a);
rowMat = zeros(1,prod(sz));
ind = 1;
for i = 1:sz(1)
j = 1;
ii = i;
while ii>0 && j<=i
rowMat(ind) = a(ii,j);
ind = ind+1;
j = j+1;
ii = ii-1;
end
if i == sz(1)
for j = 2:sz(2)
ii = i;
jj = j;
while ii>1 && jj<=i
rowMat(ind) = a(ii,jj);
ind = ind+1;
jj = jj+1;
ii = ii-1;
end
end
end
end
a
rowMat
Refer to MATLAB Onramp to get started with MATLAB.

추가 답변 (1개)

Stephen23
Stephen23 2020년 5월 27일
편집: Stephen23 2020년 5월 27일
>> a = [2,4,7,11;7,9,5,54;2,5,7,9;12,41,45,21]
a =
2 4 7 11
7 9 5 54
2 5 7 9
12 41 45 21
>> S = size(a);
>> V = 1-S(1):S(2)-1;
>> F = @(d)diag(flipud(a),d);
>> b = cell2mat(arrayfun(F,V(:),'uni',0)).'
b =
2 7 4 2 9 7 12 5 5 11 41 7 54 45 9 21

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by