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

2 ビュー (過去 30 日間)
Kaufkj44
Kaufkj44 2021 年 8 月 2 日
コメント済み: darova 2021 年 8 月 3 日
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).
darova
darova 2021 年 8 月 3 日
Happy to help

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by