Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Help to vectorize a loop with square matrix
Replies: 5   Last Post: Jul 27, 2012 11:32 AM

 Messages: [ Previous | Next ]
 Brandon Posts: 33 Registered: 2/28/05
Re: Help to vectorize a loop with square matrix
Posted: Jul 27, 2012 10:37 AM

On Thursday, July 26, 2012 5:04:24 PM UTC-4, Brandon wrote:
> It&#39;s been some time since I&#39;ve used Matlab, but I need to vectorize the following code for performance reasons. I&#39;m getting caught up on the square matrix R. I understand the concept of replicating vectors to form arrays using repmat() or multiplying vectors by ones(), but I can&#39;t wrap my head around this one, thanks.
>
> &lt;SNIP&gt;
> N = 4;
> angle_deg = 0;
> angles = -180:0.1:180;
> g = zeros(size(angles, 2), 1);
> for k = 1:length(angles)
> theta = angles(k);
> % [N x 1]
> n = (0:N-1).&#39;;
> % [N x 1]
> x = exp(-1j*pi*n*sin( (theta-angle_deg)*pi/180));
> % [N x N]
> R = (x * x&#39;);
> % [N x 1]
> w = ones(N,1)/N;
> % [1 x N]
> wh = w&#39;;
> % [1 x 1] = [1 x N]*[N x N]*[N x 1]
> tmp = wh * R * w;
> % [K x 1]
> g(k) = tmp;
> end % for k = 1:length(angles)
> &lt;/SNIP&gt;

Ok weird I must have had a typo or pasted incorrectly. It looks fine now. I don't quite understand the methodology you applied to implement it however. Could you describe the process? Thanks.

Date Subject Author
7/26/12 Brandon
7/26/12 Bruno Luong
7/27/12 Brandon
7/27/12 Bruno Luong
7/27/12 Brandon
7/27/12 Bruno Luong