Filter löschen
Filter löschen

Different variable multiplication for different columns in a MATRIX

1 Ansicht (letzte 30 Tage)
NIRBAN CHAKRABORTY
NIRBAN CHAKRABORTY am 11 Apr. 2022
Beantwortet: KuriGohan am 11 Apr. 2022
Suppoose I have a (6*10) matrix name as A. Now i want to multiply the 1st 5 column with 2 and next 5 column with 3 of A matrix to develop a new B matrix. How I going to write that code of multiplication. PLease help me out in this.

Antworten (3)

Voss
Voss am 11 Apr. 2022
A = randi(10,[6 10])
A = 6×10
7 5 7 3 1 2 9 8 3 6 3 3 4 8 8 1 7 9 8 2 9 1 2 5 1 1 3 1 1 5 7 7 1 7 1 3 5 10 2 10 3 7 6 10 7 8 1 6 4 3 9 10 10 9 1 6 2 7 3 4
% one way:
factor = [2*ones(1,5) 3*ones(1,5)]
factor = 1×10
2 2 2 2 2 3 3 3 3 3
B = A.*factor
B = 6×10
14 10 14 6 2 6 27 24 9 18 6 6 8 16 16 3 21 27 24 6 18 2 4 10 2 3 9 3 3 15 14 14 2 14 2 9 15 30 6 30 6 14 12 20 14 24 3 18 12 9 18 20 20 18 2 18 6 21 9 12
% another way:
factor = repelem([2 3],1,5)
factor = 1×10
2 2 2 2 2 3 3 3 3 3
B = A.*factor
B = 6×10
14 10 14 6 2 6 27 24 9 18 6 6 8 16 16 3 21 27 24 6 18 2 4 10 2 3 9 3 3 15 14 14 2 14 2 9 15 30 6 30 6 14 12 20 14 24 3 18 12 9 18 20 20 18 2 18 6 21 9 12

David Hill
David Hill am 11 Apr. 2022
a=randi(10,5,10);
b=a.*([ones(1,5)*2,ones(1,5)*3]);

KuriGohan
KuriGohan am 11 Apr. 2022
There is probably an easier way to do this; but this is what i would do:
a1 = a(:,1:5) .* 2;
a2 = a(:,6:end).*3;
b = vertcat(a1,a2);

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by