MATLAB Vectorizing Simple Loops for Faster Execution Time in MATLAB

  • Thread starter Thread starter vijetha9
  • Start date Start date
  • Tags Tags
    Loops
AI Thread Summary
The discussion revolves around vectorizing a MATLAB code snippet that generates a 3-D matrix containing randomly generated complex numbers. The original code uses a loop to create three 2-D matrices filled with complex numbers, but the user seeks a more efficient, vectorized solution. The proposed solution involves generating a 3-D matrix directly using the expression h = randn(3,3,3) + j * randn(3,3,3). This approach eliminates the loop, potentially improving execution time. The user confirms that the vectorized code works as intended, indicating a successful resolution to the problem.
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

Back
Top