How to reshape matrix without repeating values of matrix?

조회 수: 5 (최근 30일)
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2022년 1월 6일
댓글: SANDEEP SINGH RANA 2022년 1월 6일
Hi, For example we have a matrix 4 x 2 and we have to rearrange matrix such that
columns
rows 2 5 % 2 can pair with any below value except 5 or 5 can pair with any value except 2
4 3 % 4 can pair any value below except 3 and above pair
1 6 % 1 can pair with any below value except 6 and above pairs
7 8 % 7 can pair with any below value except 8 and above pairs ( as in this case only one value will be
remains)
This logic should work on any [] x 2 matrix.
Thanks and Regards,
Sandeep

채택된 답변

KSSV
KSSV 2022년 1월 6일
A = [2 5
4 3
1 6
7 8];
[m,n] = size(A) ;
iwant = zeros(m,m) ;
iwant(:,1) = A(:,1) ;
for i = 1:m
idx = setdiff(1:m,i) ;
iwant(i,2:end) = A(idx,2)' ;
end
iwant
iwant = 4×4
2 3 6 8 4 5 6 8 1 5 3 8 7 5 3 6
  댓글 수: 3
KSSV
KSSV 2022년 1월 6일
I gave you all the options possible...pick any randmoly...easy:
A = [2 5
4 3
1 6
7 8];
[m,n] = size(A) ;
iwant = zeros(m,2) ;
iwant(:,1) = A(:,1) ;
for i = 1:m
idx = randsample(setdiff(1:m,i),1) ;
iwant(i,2) = A(idx,2) ;
end
iwant
iwant = 4×2
2 8 4 8 1 5 7 5
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2022년 1월 6일
In this matrix, 2 is pair with 8 and 4 is also pairing with 8. This is not required.

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

추가 답변 (0개)

카테고리

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