Is there a better way to write this in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter czechman45
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around optimizing matrix multiplication in MATLAB, specifically multiplying slices of a 3D matrix with corresponding vectors. Participants explore alternatives to using a for loop for this operation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the problem of multiplying a 3D matrix A (3x3x10) with a 2D matrix C (3x10) and the error encountered when attempting element-wise multiplication.
  • Another participant suggests using the repmat function as a potential solution, although specifics are not provided.
  • There is a discussion about the robustness of using a for loop, with one participant arguing that it is a reliable method for this task.
  • A suggestion is made to modify the loop index to accommodate varying sizes of the 3D matrix A, enhancing the flexibility of the code.
  • One participant emphasizes the importance of using variables in code to improve reusability and maintainability.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and efficiency of using a for loop versus alternative methods, with no consensus on a single best approach.

Contextual Notes

Participants do not provide a definitive solution to avoid the for loop, and the discussion remains open regarding the best practices for matrix operations in MATLAB.

czechman45
Messages
12
Reaction score
0
I have two matricies.
A is a 3x3x10. C is a 3x10. (A is a changing 3x3 rotation matrix and C is a changing velocity vector)
I want to multiply each 3x3 part of A with the respective 3x1 part of C.

I tried A.*C, but I get the following error: Error using ==> times Number of array dimensions must match for binary array op.
This worked:

for i=1:10
A(:,:,i)*C(:,i)
end

is there a way to not have to do it in a for loop though?

Thanks for your help.
 
Physics news on Phys.org
czechman45 said:
I have two matricies.
A is a 3x3x10. C is a 3x10. (A is a changing 3x3 rotation matrix and C is a changing velocity vector)
I want to multiply each 3x3 part of A with the respective 3x1 part of C.

I tried A.*C, but I get the following error: Error using ==> times Number of array dimensions must match for binary array op.
This worked:

for i=1:10
A(:,:,i)*C(:,i)
end

is there a way to not have to do it in a for loop though?

Thanks for your help.

I guess you must use repmat function...check on the help
 
czechman45 said:
This worked:

for i=1:10
A(:,:,i)*C(:,i)
end

is there a way to not have to do it in a for loop though?

Thanks for your help.

Short of doing it 10 times by hand. No. Why wouldn't you want to do it by a loop though. That way is most robust...

You can change
Code:
 i=1:10

to
Code:
 i=1:size(A,3)

In order to make it so no matter the amount of 3x3 matrices you have, you don't have to change the loop variable...
 
viscousflow said:
Short of doing it 10 times by hand. No. Why wouldn't you want to do it by a loop though. That way is most robust...

You can change
Code:
 i=1:10

to
Code:
 i=1:size(A,3)

In order to make it so no matter the amount of 3x3 matrices you have, you don't have to change the loop variable...

I don't know too much about MATLAB (just started this semester), but this is the best advice I could give you as a "programmer." Always and I mean ALWAYS write in your code so that things are variables. That way, if you need to re-use your code, you can just change 1 number instead of 500 numbers in every line of your code.

Besides the i =1:size thing, I can't see a faster way of doing it, but I just thought I'd add that it is almost always better to do things with variables (and long-named variables at that so you know what the variable is for).
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K