How do you change an entire row or column of an array to a certain value

151 ビュー (過去 30 日間)
Shawn Blancett
Shawn Blancett 2017 年 9 月 15 日
コメント済み: Star Strider 2023 年 9 月 13 日
If I have an array of all zeros X = zeros(N) How do I change a column to a value I want.

採用された回答

Star Strider
Star Strider 2017 年 9 月 15 日
One option:
N = 5;
X = zeros(N);
X(:,3) = rand(N, 1); % Change Column #3 To A Specific Vector
X(2,:) = 5*ones(1, N); % Change Row #2 To a Row Of ‘5’
  4 件のコメント
Riley Harris
Riley Harris 2023 年 9 月 13 日
Can this be done using a for loop and if/elseif statements?
Star Strider
Star Strider 2023 年 9 月 13 日
@Riley Harris — Yes, however it can easily be vectorised. Using the sub2ind function is another option.
See the documentation section on Matrix Indexing for details.

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

その他の回答 (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