How to change the values in a matrix

3 ビュー (過去 30 日間)
Cameron Lissarrague
Cameron Lissarrague 2020 年 2 月 18 日
回答済み: Star Strider 2020 年 2 月 18 日
I have created a for loop to create a matrix,
n = input("Enter number of rows for matrix ");
m = input("Enter number of columns for matrix ");
a = zeros(n:m);
x = n*m;
for i = 1:x
if i == 1
a(i) = 1;
elseif i > 1 && i < n
a(i) = 8;
elseif i == n
a(i) = 7;
elseif i == x
a(i) = 5;
elseif i == (x-(n-1))
a(i) = 3;
elseif i > (x-(n-1)) && i < x
a(i) = 4;
elseif i == x
a(i) = 5;
else
a(i) = 9;
end
end
The matrix created is
Enter number of rows for matrix 5
Enter number of columns for matrix 5
1 9 9 9 3
8 9 9 9 4
8 9 9 9 4
8 9 9 9 4
7 9 9 9 5
Now I wish to change the values of the first row to 2's excluding the first and last value in the row. If any body is able to help it will be greatly appriciated!
Thank you

採用された回答

Star Strider
Star Strider 2020 年 2 月 18 日
One approach:
Before = [ 1 9 9 9 3
8 9 9 9 4
8 9 9 9 4
8 9 9 9 4
7 9 9 9 5]
After = Before;
After(1,2:end-1) = ones(size(Before(1,2:end-1)))*2
producing:
After =
1 2 2 2 3
8 9 9 9 4
8 9 9 9 4
8 9 9 9 4
7 9 9 9 5

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by