MATLAB Answers

Construct a 5 x 10 array that contains zeros. Assign elements at (row, column) (3,5), (2,4) and (1, 9) values of 44. Display the matrix.

4 ビュー (過去 30 日間)
Eiman Rana
Eiman Rana 2021 年 9 月 16 日
コメント済み: Eiman Rana 2021 年 9 月 16 日
I am unable to assign the values to the specific element
  4 件のコメント
Eiman Rana
Eiman Rana 2021 年 9 月 16 日
Could you give me an example of both simultaneously and sequentially

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

回答 (1 件)

DGM
DGM 2021 年 9 月 16 日
編集済み: DGM 2021 年 9 月 16 日
Consider an altered example:
In a 3x5 array, assign a fixed value to elements (3,3), (2,5), and (1,1)
sz = [3 5]; % array size
A = zeros(sz);
A(sub2ind(sz,[3 2 1],[3 5 1])) = 37
A = 3×5
37 0 0 0 0 0 0 0 0 37 0 0 37 0 0
This uses linear indexing to do the job. In order to calculate the indices, sub2ind() is used to convert the row and column subscripts specified in the two vectors. Linear indices are calculated column-major, so the indices for this example would be [9 14 1].

Community Treasure Hunt

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

Start Hunting!

Translated by