Multiply only certain columns in a matrix with a constant

1 view (last 30 days)
Hello,
I have a 100x9 matrix M, whose last six columns should be multiplied by a constant c and the first three columns shall remain unchanged. What would be an appropriate way to achieve this?
Thank you very much for your help!

Accepted Answer

Star Strider
Star Strider on 28 Jul 2022
Another approach —
M = rand(5,9) % Shorter Version Of 'M'
M = 5×9
0.7412 0.4804 0.7538 0.9349 0.9716 0.1276 0.5271 0.2733 0.5096 0.1520 0.6185 0.9599 0.8982 0.1722 0.2804 0.0377 0.0279 0.3176 0.0054 0.8282 0.3499 0.7655 0.1470 0.0558 0.5818 0.7755 0.2665 0.7992 0.0395 0.8887 0.5188 0.6443 0.1080 0.8812 0.9678 0.3667 0.5886 0.9454 0.6386 0.2229 0.4535 0.2106 0.0983 0.3908 0.9509
c = 1000; % Create 'c'
Mnew = M.*[ones(1,3) ones(1,6)*c] % Multiply
Mnew = 5×9
0.7412 0.4804 0.7538 934.8765 971.5514 127.5821 527.1396 273.2641 509.6324 0.1520 0.6185 0.9599 898.1878 172.1874 280.4265 37.6542 27.8550 317.6187 0.0054 0.8282 0.3499 765.5312 147.0171 55.8182 581.7670 775.5124 266.5422 0.7992 0.0395 0.8887 518.8324 644.2832 107.9574 881.2123 967.8147 366.6843 0.5886 0.9454 0.6386 222.9475 453.5469 210.5717 98.2724 390.7578 950.9357
.

More Answers (1)

dpb
dpb on 28 Jul 2022
M(:,end-5:end)=c*M(:,end-5:end);

Tags

Community Treasure Hunt

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

Start Hunting!