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

Click For Summary
The discussion revolves around optimizing matrix multiplication in MATLAB without using a for-loop, specifically multiplying each component of vector B by matrix A and solving A\C. The original poster seeks a more efficient solution due to performance concerns, particularly with a triple nested for-loop in their code. Respondents note that while for-loops were historically slow in MATLAB, recent improvements have made them more efficient, and they can enhance code readability. The conversation highlights the balance between code clarity and execution speed when dealing with matrix operations. Ultimately, the focus remains on finding a faster alternative to the for-loop for this specific matrix-vector multiplication problem.
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:
Physics news 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K