How to reshape matrix according to the given matrix?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
Let i have 10 X 1 matrix
matrix_value indices
1 - 3
2 - 5
3 - 2
4 - 1
5 - 2
6 - 4
7 - 5
8 -1
9 - 4
10 - 3
Now I have store this in matrix of size 5 x 2 such that matrix_value of same indices will be in pairs ( in this i have taken only pairs, it may be more) . Matrix_values will be like below:
row1 4 , 8
row2 3, 5
row3 1, 10
Hope you got this, I have to create loop so that next time when i will get 20 x 1 matrix with indices, I can arrange them in (10 x 2) or any ( * x 2) pair.
Thanks and Regards
0 件のコメント
採用された回答
Cris LaPierre
2021 年 12 月 31 日
4 件のコメント
Cris LaPierre
2022 年 1 月 3 日
編集済み: Cris LaPierre
2022 年 1 月 3 日
I guess the simplest approach would be to make sure all numbers appear an even number of times, then use the same approach as before. Set any indices greater than the length of the original vector to 0.
% original vector
matrix_value = [3;5;2;1;2;4;5;1;2;3];
% find number of times each value appears
[g,id] = groupsummary(matrix_value,matrix_value,"nnz")
% Append values to the bottom so all values appear an even number of times
odd = logical(rem(g,2));
matrix_value2 = [matrix_value;id(odd)];
% sort the data
[tmp,I] = sort(matrix_value2);
% reshape and transpose to be nx2
newInd = reshape(I,2,[])';
% set any indices greater than orginal length to 0
newInd(newInd>length(matrix_value)) = 0
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!