Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
simple loop problem

Subject: simple loop problem

From: payam

Date: 20 Dec, 2012 15:38:08

Message: 1 of 3

Hello all,
I put my code here so you can understand better what i am looking for. This is my code :

Lambda= 0.785;
k= 2*pi/Lambda;
S = 40;
a= 0.3;
l = 10;
theta0= atan (S/l);
n= (S/a) ;
s= theta/n ;
I0 = 100;

for i=1:4*n
    for j=1:n
        I(i,j) = I0 *(sin((k*a/2)*sin(theta0-(j-i)*0.58)))/(k*a/2)*sin(theta0-(j-i)*0.58) ;
        
    end
end

I want at the end to have data that contains : [ I(1,:), I(2,:), I(3,:), ..... , I(4n,:) ]
it means I want 4n numbers that are the sum up of all the js for each individual i.

Subject: simple loop problem

From: someone

Date: 20 Dec, 2012 17:01:08

Message: 2 of 3

"payam " <payamvahdati@yahoo.com> wrote in message <kavbd0$ji8$1@newscl01ah.mathworks.com>...
> Hello all,
> I put my code here so you can understand better what i am looking for. This is my code :
>
> Lambda= 0.785;
> k= 2*pi/Lambda;
> S = 40;
> a= 0.3;
> l = 10;
> theta0= atan (S/l);
> n= (S/a) ;
> s= theta/n ;
> I0 = 100;
>
> for i=1:4*n
> for j=1:n
> I(i,j) = I0 *(sin((k*a/2)*sin(theta0-(j-i)*0.58)))/(k*a/2)*sin(theta0-(j-i)*0.58) ;
>
> end
> end
>
> I want at the end to have data that contains : [ I(1,:), I(2,:), I(3,:), ..... , I(4n,:) ]
> it means I want 4n numbers that are the sum up of all the js for each individual i.

% I don't have MATLAB installed on this computer,
% but one of:

doc sum
doc cumsum

% should help you and get you started.
% BTW, there are many improvements you can make to the above code.
% For one, I suggest preallocating I. Something like:

I = zeros(size(4*n,n))

% before the for loop.

Subject: simple loop problem

From: Roger Stafford

Date: 20 Dec, 2012 20:01:12

Message: 3 of 3

"payam " <payamvahdati@yahoo.com> wrote in message <kavbd0$ji8$1@newscl01ah.mathworks.com>...
> for i=1:4*n
> for j=1:n
> I(i,j) = I0 *(sin((k*a/2)*sin(theta0-(j-i)*0.58)))/(k*a/2)*sin(theta0-(j-i)*0.58) ;
> end
> end
>
> I want at the end to have data that contains : [ I(1,:), I(2,:), I(3,:), ..... , I(4n,:) ]
> it means I want 4n numbers that are the sum up of all the js for each individual i.
- - - - - - - - - - - -
 [J,I] = ndgrid(1:n,1:4*n);
 D = sum(I0*(sin((k*a/2)*sin(theta0-(J-I)*0.58)))/(k*a/2).*sin(theta0-(J-I)*0.58),1);

  Note: Are you sure about your parentheses? As it is, you are multiplying by that rightmost sine, not dividing by it.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us