Create a matrix on the basis of other matrix

1 回表示 (過去 30 日間)
luca
luca 2019 年 10 月 14 日
コメント済み: Fabio Freschi 2019 年 10 月 15 日
Hi given a vector
SPI= [2 3 4 8 11 13 14 15 16 19 20];
and the array
AA= [1 2 3 4 0 11 15;
0 0 0 8 13 16 0;
0 0 0 0 0 18 0;
0 0 0 0 0 19 0;
0 0 0 0 0 20 0];
I want to create a matrix DD that is AA but with just the element inside SPI. So:
DD = [ 2 3 4 0 11 15;
0 0 8 13 16 0;
0 0 0 0 0 0;
0 0 0 0 19 0;
0 0 0 0 20 0];
may someone help me?
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 14 日
I notice that you delete the first column of AA as it is left without any elements from SPI. Why are you not also deleting the all-zero row 3 ? Or if the rule is that the first row has to contain an element from SPI, why are you not deleting the column [0; 13; 0; 0; 0] ? What are the rules?
luca
luca 2019 年 10 月 14 日
編集済み: luca 2019 年 10 月 14 日
the array SPI could vary and contain all the elements of AA, that instead is fixed. So depending on the value inside SPI I want to derive DD.
Sorry for the third column, was my mistake! now it's fixed

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

採用された回答

Fabio Freschi
Fabio Freschi 2019 年 10 月 14 日
編集済み: Fabio Freschi 2019 年 10 月 14 日
% your data
SPI = [2 3 4 8 11 13 14 15 16 18 19 20];
AA = [1 2 3 4 0 11 14 15;
0 0 0 8 13 16 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 19 0 0;
0 0 0 0 0 20 0 0];
% mask
iMask = ismember(AA,SPI);
% matrix DD with zeros
DD = AA.*iMask;
% now you can filter out the rows and cols of all zeros
DD = DD(:,any(iMask,1)); % cols filter
DD = DD(any(iMask,2),:); % rows filter
  6 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 15 日
Fabio's suggested code will eliminate the row 0 0 0 0 0 0;
I do not understand at the moment why that row is being kept in the desired solution.
Fabio Freschi
Fabio Freschi 2019 年 10 月 15 日
Me neither! but I kept all steps separated to that the OP can pick the parts of his choice

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by