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

In summary, the user is trying to solve a problem by multiplying each component of B by the matrix A and then solving A\C. They have provided a code with a for-loop but are wondering if there is a more efficient way to do it without a for-loop. The expert suggests that using a for-loop may actually be easier and faster, but the user is hesitant due to previous experiences with slow for-loops.
  • #1
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
  • #2
Thought I knew how but I was wrong. Still open for your answers :oldbiggrin:
 
Last edited by a moderator:
  • #3
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.
 
  • #4
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.
 

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

1. How can matrix multiplication be performed without using a for-loop?

The most common approach to matrix multiplication without a for-loop is by using vectorization. This involves using linear algebra operations on arrays rather than iterating through each element with a for-loop. This method is more efficient and faster for large matrices.

2. Can matrix multiplication be performed for an uneven size matrix and a vector?

Yes, matrix multiplication can be performed for an uneven size matrix and a vector. In this case, the number of columns in the matrix must be equal to the number of rows in the vector. The resulting matrix will have the same number of rows as the original matrix and a single column.

3. What is the difference between matrix multiplication with a for-loop and without a for-loop?

The main difference between matrix multiplication with a for-loop and without a for-loop is in the approach used. With a for-loop, the multiplication is done element by element, while without a for-loop, vectorization is used to perform the operation on the entire matrix at once, making it more efficient.

4. Are there any limitations to using matrix multiplication without a for-loop?

One limitation of using matrix multiplication without a for-loop is that it requires an understanding of linear algebra operations and vectorization techniques. This may be challenging for those who are not familiar with these concepts.

5. How does matrix multiplication without a for-loop compare to traditional matrix multiplication in terms of performance?

Matrix multiplication without a for-loop is generally more efficient and faster than traditional matrix multiplication. This is because it eliminates the need for iteration and instead performs the operation on the entire matrix at once using vectorization techniques.

Similar threads

Replies
2
Views
1K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
3
Views
2K
Replies
6
Views
2K
Replies
5
Views
5K
Back
Top