Matrix multiplication without a for-loop for an uneven size matrix and a vector

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
member 428835
Hi PF!

I am trying to multiply each component of B by the matrix A and then solve A\C. See the code below.

Matlab:
A = rand(4);
B = rand(5,1);
C = rand(4,1);
for i = 1:5
 sol(:,i) = (B(i)*A)\C
end

But there has to be a way to do this without a for-loop, right? I'd really appreciate any help you have!
 
Last edited by a moderator:
on Phys.org
Thought I knew how but I was wrong. Still open for your answers :oldbiggrin:
 
Last edited by a moderator:
May I ask WHY you do not want to use a FOR loop? Years ago, FOR loops in Matlab were extremely slow but this is no longer true and sometimes using FOR loops makes the code much easier to understand.
This is certainly true here since B is not really used as a vector in your example.
 
f95toli said:
May I ask WHY you do not want to use a FOR loop? Years ago, FOR loops in Matlab were extremely slow but this is no longer true and sometimes using FOR loops makes the code much easier to understand.
This is certainly true here since B is not really used as a vector in your example.
For speed. I have a triple nested for loop and was hoping to get things a little faster.