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

3 次查看(过去 30 天)
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!

回答(1 个)

darova
darova 2021-8-2
Read about mat2cell
  2 个评论
Kaufkj44
Kaufkj44 2021-8-2
I think I see what you're implying(?). I'd first bookend matrix A with NaNs through concatenation and from there use a fixed range (size(B,2)) and mat2cell to extrapolate the relevant values from A. Was this what you had in mind?
If I didn't concatenate a NaN matrix, I wouldn't have a fixed range of values for each row and I don't believe mat2cell would be helpful then (e.g., rows 1 and 2 of B grabbed 7 and 4 elements of A, respectively).

请先登录,再进行评论。

类别

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