How can i index a matrix to do implicit multiplication to each value in a row with a vector?

9 次查看(过去 30 天)
I have a matrix, A of size (a x b) and a vector s of size (1 x b) and I want to multiply each row of A with s.
I am trying to use a for loop to index each of the 'a' rows of matrix A and then index along the 'b' columns and multiply each value with the corresponding index of vecor s. This should then return a new matrix B that is the same size as matrix A, however I cannot get this to work. This is my code;
A = rand(8,3);
s = rand(1,3);
for i = 1:size(A,1)
c = A(i,:);
for ii = 1:size(A,2)
x(ii) = c(ii)*s(ii);
end
end
Please note: I do not want to simply multiply the two together, I am aware that i can transpose and get a resulting a x 1 size vector - this is not the solution I want to achieve.

采纳的回答

David Hill
David Hill 2019-9-21

更多回答(0 个)

类别

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

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by