Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply sub-matrix by a constant

Tags:

matlab

Lets say I have matrix

a =  [ 1 1 1 1;
       1 1 1 1;
       1 1 1 1]

And I would like to multiply the sub-matrix a(2:3, 2:3) by 5; So that the new matrix is

a =  [ 1 1 1 1;
       1 5 5 1;
       1 5 5 1]

What function does that ? I've tried this = >

a = a(2:3, 2:3)*5;

But that would just give me a new matrix 2x2

a = [5 5;
     5 5]
like image 278
Tony Tannous Avatar asked Feb 09 '23 03:02

Tony Tannous


1 Answers

You need to specify the target correctly.

A=ones(6,6);
A(3:4,3:4) = A(3:4,3:4)*5

A =
 1     1     1     1     1     1
 1     1     1     1     1     1
 1     1     5     5     1     1
 1     1     5     5     1     1
 1     1     1     1     1     1
 1     1     1     1     1     1
like image 151
rst Avatar answered Feb 15 '23 05:02

rst