필터 지우기
필터 지우기

creating a data array from a matrix

조회 수: 7 (최근 30일)
Damith
Damith 2014년 5월 30일
댓글: Damith 2014년 5월 31일
Hi,
I have matrix1 of 39 x 39. I need to create X and Y arrays by reading elements from rows and columns of matrix1 in a certain way.
X Y
(2,1) (1,2)
(3,1) (1,3)
(4,1) (1,4)
..
(39,1) (1,39)
(3,2) (2,3)
(4,2) (2,4)
..
(39,2) (2,39)
(4,3) (3,4)
..
(39,3) (3,39)
..
(39,38) (38,39)
How can I implement in MATLAB? Can somebody help me.
Thanks in advance,

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 5월 30일
편집: Geoff Hayes 2014년 5월 30일
Hi Damith - I'd go with a double for loop, pre-allocating memory for the X and Y vectors:
[m,n] = size(matrix1); % where matrix1 is the 39x39 matrix
numElems = (m-1)*m/2; % since ignoring elements on matrix diagonal
X = zeros(numElems,1);
Y = X;
at = 1;
for i=1:m
for j=i+1:m % since not interested in i==j
X(at) = matrix1(j,i);
Y(at) = matrix1(i,j);
at = at + 1;
end
end
  댓글 수: 1
Damith
Damith 2014년 5월 30일
Thanks a lot. It worked.

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

추가 답변 (1개)

Udit Gupta
Udit Gupta 2014년 5월 30일
This is simple if you use matlab lower triangle functions. See the example code below please.
A = magic(39) % Your Martix
mask = tril(true(39),-1);
X = A(mask);
B = A';
Y = B(mask);
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2014년 5월 30일
That's neat!
Damith
Damith 2014년 5월 31일
Great! Both works. Thanks guys.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by