How to assign a constant to a submatrix without a loop

1 次查看(过去 30 天)
Is this possible without a for loop? Sample code:
%A is an (m x n) matrix m,n > 2
A(2:end-1,2) = 67.5*ones(size(A(2:end-1,2)));
I so far can not make this work.
  1 个评论
Adam Danz
Adam Danz 2020-4-29
编辑:Adam Danz 2020-4-30
What's the problem with the code in your question other than that it can be simplified (see answer)?
A =
5 4 9 2 8 8
1 6 1 10 7 3
2 5 8 4 4 8
8 6 1 10 9 6
6 9 3 2 5 7
>> A(2:end-1,2) = 67.5*ones(size(A(2:end-1,2)))
A =
5 4 9 2 8 8
1 67.5 1 10 7 3
2 67.5 8 4 4 8
8 67.5 1 10 9 6
6 9 3 2 5 7

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-4-29
编辑:Adam Danz 2020-5-4
A = magic(9) % demo matrix
A(2:end-1,2) = 67.5;
result
A(2:end-1,2) = 67.5
A =
47 58 69 80 1 12 23 34 45
57 67.5 79 9 11 22 33 44 46
67 67.5 8 10 21 32 43 54 56
77 67.5 18 20 31 42 53 55 66
6 67.5 19 30 41 52 63 65 76
16 67.5 29 40 51 62 64 75 5
26 67.5 39 50 61 72 74 4 15
36 67.5 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by