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

Click For Summary

Discussion Overview

The discussion revolves around the problem of performing matrix multiplication between a matrix A and a vector B without using a for-loop in MATLAB. Participants explore the implications of avoiding for-loops, particularly in the context of performance and code clarity.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet attempting to multiply each component of vector B by matrix A and solve for C using a for-loop.
  • Another participant expresses uncertainty about the necessity of avoiding for-loops, suggesting that they may not be as slow as previously thought and can enhance code readability.
  • A different participant reiterates the question about avoiding for-loops, emphasizing the desire for improved speed due to the presence of a triple nested for-loop in their code.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the necessity of avoiding for-loops, with differing opinions on their performance and clarity in this context.

Contextual Notes

The discussion does not address specific mathematical properties or assumptions related to the matrices and vectors involved, nor does it resolve the performance implications of using for-loops versus vectorized operations.

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
4K
  • · 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