Is there a better way to write this in MATLAB?

  • MATLAB
  • Thread starter czechman45
  • Start date
  • Tags
    Matlab
In summary, the programmer is suggesting that the best way to do this particular task is to use variables.
  • #1
czechman45
12
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
  • #2
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
 
  • #3
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...
 
  • #4
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).
 
  • #5


Yes, there is a better way to write this in MATLAB. You can use the "bsxfun" function to perform element-wise multiplication on arrays with different dimensions. The syntax would be:

result = bsxfun(@times, A, permute(C, [3 2 1]));

This will perform the multiplication for each 3x3 part of A with the respective 3x1 part of C, without the need for a for loop. The "permute" function is used to rearrange the dimensions of C to match the dimensions of A.
 

1. What are some common mistakes when writing MATLAB code?

Some common mistakes when writing MATLAB code include not using vectorized operations, using inefficient loops, and not preallocating arrays. It is important to use vectorized operations whenever possible to improve code performance. Additionally, using efficient loops or avoiding loops altogether can also greatly improve code speed. Finally, preallocating arrays can prevent unnecessary memory allocation and improve code efficiency.

2. How can I improve the readability of my MATLAB code?

To improve the readability of MATLAB code, it is important to use meaningful variable names, add comments to explain the purpose of the code, and break up long sections of code into smaller, more manageable chunks. It is also helpful to follow consistent formatting and indentation practices.

3. How can I optimize my MATLAB code for speed?

To optimize MATLAB code for speed, you can use vectorized operations, preallocate arrays, and avoid unnecessary loops. You can also use built-in functions instead of writing your own code and take advantage of parallel computing if your computer has multiple processors.

4. Can I use object-oriented programming in MATLAB?

Yes, MATLAB supports object-oriented programming (OOP). You can create classes and objects, define methods and properties, and use inheritance to create more complex programs. OOP can be useful for organizing and managing large and complex code projects.

5. Are there any resources for learning how to write better MATLAB code?

Yes, there are many resources available for learning how to write better MATLAB code. The official MATLAB documentation, online tutorials, and forums are great places to start. You can also find books and online courses specifically focused on improving MATLAB coding skills.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Linear and Abstract Algebra
Replies
6
Views
516
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • Special and General Relativity
5
Replies
146
Views
6K
  • Linear and Abstract Algebra
Replies
1
Views
924
Back
Top