insert ones at given position of matrix

15 ビュー (過去 30 日間)
Phillip
Phillip 2017 年 12 月 4 日
コメント済み: Phillip 2017 年 12 月 4 日
I'm stuck on this probably very simple thing - I have this matrix:
rd = zeros(4, 6);
and x is meant to be the position I would like to insert a 1:
x = [3 1 3 1 1 1];
to obtain this:
0 1 0 1 1 1;
0 0 0 0 0 0;
1 0 1 0 0 0;
0 0 0 0 0 0;
Would anybody please be able to help?

採用された回答

Stephen23
Stephen23 2017 年 12 月 4 日
編集済み: Stephen23 2017 年 12 月 4 日
Use sub2ind:
>> rd = zeros(4,6);
>> x = [3,1,3,1,1,1];
>> rd(sub2ind(size(rd),x,1:numel(x))) = 1
rd =
0 1 0 1 1 1
0 0 0 0 0 0
1 0 1 0 0 0
0 0 0 0 0 0
  1 件のコメント
Phillip
Phillip 2017 年 12 月 4 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by