How to assign a constant to a submatrix without a loop

2 views (last 30 days)
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 Comment
Adam Danz
Adam Danz on 29 Apr 2020
Edited: Adam Danz on 30 Apr 2020
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

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 29 Apr 2020
Edited: Adam Danz on 4 May 2020
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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!