How do I create a matrix based on another matrix?

11 ビュー (過去 30 日間)
Nevzat Can Yerlikaya
Nevzat Can Yerlikaya 2021 年 1 月 19 日
コメント済み: Matt J 2021 年 1 月 19 日
Hi everyone,
I have Number of elements which is a input and have an N matrix which is also input. For example, I have NumElem=4 and corrosponding N matrix as N=[1 0 1 0; 3 0 1 1].
What I am trying to do is create a matrix with NumElem rows and 3 columns. Those 3 colums came from N column numbers 2,3,4. For previous example, the matrix would be E=[0 1 0; 0 0 0; 0 1 1; 0 0 0] it takes each N row's first value and assign it as a row in E column. Later, it assign rest of the each N row values to E row but if there is no value for specific row (for example, there is no 2 value at start of any row in matrix N) the corrosponding row (row 2 in matrix E) has all the zero values.
I know i need to use for function but i cannot figure how to obtain matrix row with all zeros.

採用された回答

Matt J
Matt J 2021 年 1 月 19 日
NumElem=4;
N=[1 0 1 0; 3 0 1 1];
E=zeros(NumElem,3);
E(N(:,1),:)=N(:,2:end)
E = 4×3
0 1 0 0 0 0 0 1 1 0 0 0
  2 件のコメント
Nevzat Can Yerlikaya
Nevzat Can Yerlikaya 2021 年 1 月 19 日
Thank you so much! This does exactly what I wanted.
Matt J
Matt J 2021 年 1 月 19 日
You're welcome, but please Accept-click the answer to certify that.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by