Vectorizing Simple Loops for Faster Execution Time in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter vijetha9
  • Start date Start date
  • Tags Tags
    Loops
Click For Summary
SUMMARY

The discussion focuses on vectorizing MATLAB code to improve execution time when generating a 3-D matrix of complex numbers. The original code uses a loop to create three 2-D matrices filled with random complex numbers, while the proposed solution utilizes a single call to the 'randn' function to generate the entire 3-D matrix at once. This method is confirmed to work effectively, enhancing performance by eliminating the loop.

PREREQUISITES
  • Understanding of MATLAB programming language
  • Familiarity with complex numbers in MATLAB
  • Knowledge of matrix operations in MATLAB
  • Experience with random number generation using 'randn' function
NEXT STEPS
  • Explore MATLAB's vectorization techniques for performance optimization
  • Learn about advanced matrix manipulation functions in MATLAB
  • Investigate profiling tools in MATLAB to measure execution time
  • Study the differences between 'rand' and 'randn' functions in MATLAB
USEFUL FOR

MATLAB programmers, data scientists, and engineers looking to optimize code performance and improve execution speed in numerical computations.

vijetha9
Messages
2
Reaction score
0
Hie all

Can anyone please help me with this problem

I need to vectorize the following MATLAB code :
for c=1:3
h = randn(3,3) + j * randn(3,3);
z( :, :,c) = h;
end

Im basically trying to create a 3-D matrix - which has 3 , 2-D matrices (these matrices contain radomly generated complex numbers. The above code is working but i want to vectorize it for faster execution time.

Thanx
 
Last edited:
Physics news on Phys.org
Is there any problem with this:

Code:
h = randn(3,3,3) + j * randn(3,3,3);

EDIT: I missed that you used 'randn' as opposed to 'rand'... I think it will work either way...

-Kerry
 
Last edited:
thanks.. it works...
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K