MATLAB Answers

How can I condense a larger matrix into a smaller matrix based on elements of a vector?

2 views (last 30 days)
Kaufkj44
Kaufkj44 on 2 Aug 2021
Commented: darova on 3 Aug 2021
Say I have the following matrix and vector:
A=[10,66,45,17,40,38,11,95,42,30;82,52,43,39,51,47,37,92,55,44;15,72,13,82,63,17,34,27,98,19;22,55,88,99,77,44,66,33,11,99];
V=[6,1,3,8];
And I want to use the elements of V to rearrange the rows of A to create a new matrix that is a sorted and condensed version of A. By sorting, I would like a given element of V to align a given row of A into the middle column index of the new matrix and for all other elements in the new matrix to be NaNs. Thus, if new matrix B is a 4x7 matrix, then the 4th column of B would equal the 6th, 1st, 3rd, and 8th elements of rows 1,2,3,and 4 of matrix A, respectively. The entire output would be the following:
B =
45 17 40 38 11 95 42
NaN NaN NaN 82 52 43 39
NaN 15 72 13 82 63 17
77 44 66 33 11 99 NaN
Happy to provide further examples if needed. Thanks in advance!

Answers (1)

darova
darova on 2 Aug 2021
Read about mat2cell
  2 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!