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:
multiple loops?

Subject: multiple loops?

From: CAE Emmerton

Date: 21 Dec, 2010 21:47:06

Message: 1 of 4

I'm new to Matlab with little programming experience. Here's what I'm trying to do:
I want to cycle y(1,1) through all x's (x(1,1) to x(4,1)) and then cycle y(2,1) through all x's (x(1,1) to x(4,1)). I'm trying multiple loops but I can't seem to get this to work. I'm trying to apply this through a much larger dataset, so I'm starting small:

x=[2 5 6 1]'
y=[4 5]'

here are the conditions I just made up:
if x()>y();
z()=x();
else z()=NaN;
end;

The answer hopefully would be:
z=
NaN
5
6
NaN
NaN
NaN
NaN
6
NaN

Subject: multiple loops?

From: Walter Roberson

Date: 21 Dec, 2010 22:02:51

Message: 2 of 4

On 10-12-21 03:47 PM, CAE Emmerton wrote:
> I'm new to Matlab with little programming experience. Here's what I'm trying
> to do:
> I want to cycle y(1,1) through all x's (x(1,1) to x(4,1)) and then cycle
> y(2,1) through all x's (x(1,1) to x(4,1)). I'm trying multiple loops but I
> can't seem to get this to work. I'm trying to apply this through a much larger
> dataset, so I'm starting small:
>
> x=[2 5 6 1]'
> y=[4 5]'
>
> here are the conditions I just made up:
> if x()>y();
> z()=x();
> else z()=NaN;
> end;
>
> The answer hopefully would be:
> z=
> NaN
> 5
> 6
> NaN
> NaN
> NaN
> NaN
> 6
> NaN

Hint:

for J = 1:3
   for K = 1:5
    z(K,J) = x(K) * y(J);
   end
end
z(:)

Subject: multiple loops?

From: Sean de

Date: 21 Dec, 2010 22:04:05

Message: 3 of 4

"CAE Emmerton" wrote in message <ier78q$1dj$1@fred.mathworks.com>...
> I'm new to Matlab with little programming experience. Here's what I'm trying to do:
> I want to cycle y(1,1) through all x's (x(1,1) to x(4,1)) and then cycle y(2,1) through all x's (x(1,1) to x(4,1)). I'm trying multiple loops but I can't seem to get this to work. I'm trying to apply this through a much larger dataset, so I'm starting small:
>
> x=[2 5 6 1]'
> y=[4 5]'
>
> here are the conditions I just made up:
> if x()>y();
> z()=x();
> else z()=NaN;
> end;
>
> The answer hopefully would be:
> z=
> NaN
> 5
> 6
> NaN
> NaN
> NaN
> NaN
> 6
> NaN

doc bsxfun

%%%
%Data
x=[2 5 6 1]';
y=[4 5];

%Engine
idx = bsxfun(@(x,y)(x>y),x,y);
xfull = repmat(x,[1,size(y,2)]);
z(idx) = xfull(idx);
z(~idx) = nan;
z = z.'


%SCd
%%%

Subject: multiple loops?

From: CAE Emmerton

Date: 21 Dec, 2010 23:16:05

Message: 4 of 4

"CAE Emmerton" wrote in message <ier78q$1dj$1@fred.mathworks.com>...
> I'm new to Matlab with little programming experience. Here's what I'm trying to do:
> I want to cycle y(1,1) through all x's (x(1,1) to x(4,1)) and then cycle y(2,1) through all x's (x(1,1) to x(4,1)). I'm trying multiple loops but I can't seem to get this to work. I'm trying to apply this through a much larger dataset, so I'm starting small:
>
> x=[2 5 6 1]'
> y=[4 5]'
>
> here are the conditions I just made up:
> if x()>y();
> z()=x();
> else z()=NaN;
> end;
> Great, thank you!
> The answer hopefully would be:
> z=
> NaN
> 5
> 6
> NaN
> NaN
> NaN
> NaN
> 6
> NaN

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