How to reshape matrix without repeating values of matrix?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
3 个评论
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!