I am trying to make a zeros matrix where certain places is replaced by a 1

1 回表示 (過去 30 日間)
I have created a 3x8 matrix of zeroes where i am trying to get place (1,1) and (1,8) and (3,4) to be replaced by ones. so it would look like this:
1 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
i have tried to wrote some code for this but i couldn't get it to work.
i have trid this:
b=zeros(3,8)
b([1,3],[1,4,8])=1
but this gives me a matrix that looks like this:
1 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 1

採用された回答

Chetan Bhavsar
Chetan Bhavsar 2021 年 10 月 12 日
編集済み: Chetan Bhavsar 2021 年 10 月 12 日
b=zeros(3,8);
r = [1 1 3]; % row
c = [1 8 4]; % column
idx = sub2ind(size(b),r,c);
b(idx) = 1;
b
b = 3×8
1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by