How to reshape matrix according to the given matrix?
8 views (last 30 days)
Show older comments
SANDEEP SINGH RANA
on 31 Dec 2021
Edited: Cris LaPierre
on 3 Jan 2022
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 Comments
Accepted Answer
Cris LaPierre
on 31 Dec 2021
4 Comments
Cris LaPierre
on 3 Jan 2022
Edited: Cris LaPierre
on 3 Jan 2022
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
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!