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:
making a loop

Subject: making a loop

From: Sunipa Som

Date: 31 Mar, 2011 11:00:21

Message: 1 of 3

Hello,
I have problem like this
z=2;
>> y=1:4;
>> r=sqrt(y.^2+z.^2)

r =

    2.2361 2.8284 3.6056 4.4721

>> z=3;
>> y=1:4;
>> r=sqrt(y.^2+z.^2)

r =

    3.1623 3.6056 4.2426 5.0000
I want to make a loop or programming by which it automatically read the values of z and give the result. Which will be a matrix. where y is a vector and same for each r but z is scalar and it changing for each r. How can I do it?
Thanks,
S Som

Subject: making a loop

From: wahyoe Unggul

Date: 31 Mar, 2011 12:31:05

Message: 2 of 3

"Sunipa Som" wrote in message <in1ms5$e5u$1@fred.mathworks.com>...
> Hello,
> I have problem like this
> z=2;
> >> y=1:4;
> >> r=sqrt(y.^2+z.^2)
>
> r =
>
> 2.2361 2.8284 3.6056 4.4721
>
> >> z=3;
> >> y=1:4;
> >> r=sqrt(y.^2+z.^2)
>
> r =
>
> 3.1623 3.6056 4.2426 5.0000
> I want to make a loop or programming by which it automatically read the values of z and give the result. Which will be a matrix. where y is a vector and same for each r but z is scalar and it changing for each r. How can I do it?
> Thanks,
> S Som

What do you mean like this?
z=3;
y=1:4;
for i=z
   r=sqrt(y.^2+i.^2)
end

r =

    3.1623 3.6056 4.2426 5.0000


wahyoe

Subject: making a loop

From: Matt J

Date: 31 Mar, 2011 13:15:20

Message: 3 of 3

"Sunipa Som" wrote in message <in1ms5$e5u$1@fred.mathworks.com>...
>
>
> r =
>
> 3.1623 3.6056 4.2426 5.0000
> I want to make a loop or programming by which it automatically read the values of z and give the result. Which will be a matrix. where y is a vector and same for each r but z is scalar and it changing for each r. How can I do it?
============

For example,

y=1:3;
z=4:6;


r=abs(bsxfun(@plus, y(:),i*z(:).')) ;

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