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

조회 수: 4 (최근 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에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by