# Matlab function with vector inputs and matrix outputs

• Aug 5th 2009, 12:36 AM
deragon999
Matlab function with vector inputs and matrix outputs
Hi all,

I need to write a matlab function file (in editor) for a cylinder.
The input values will be vectors containing the heights and radii of various different cylinders, and the outputs will be surface area, cross sectional area, and volume in a matrix.

I know how to write a function with singular inputs, but i can't get vector inputs to work.

Any help would be appreciated.
• Aug 6th 2009, 05:07 AM
CaptainBlack
Quote:

Originally Posted by deragon999
Hi all,

I need to write a matlab function file (in editor) for a cylinder.
The input values will be vectors containing the heights and radii of various different cylinders, and the outputs will be surface area, cross sectional area, and volume in a matrix.

I know how to write a function with singular inputs, but i can't get vector inputs to work.

Any help would be appreciated.

Do you want the input to be d=[height, radius] or: d=[height1, radius1;height2, radius2; ...]?

CB
• Aug 6th 2009, 05:40 PM
deragon999
I want it like this:

function [surface_area area volume]=cylinder(height,radius)

So that the following can be typed into matlab command:

which will result in an output of:

surfacearea1 area1 volume1
surfacearea2 area2 volume2
surfacearea3 area3 volume3
etc etc etc

If the output matrix has to be aligned a different way(my examples rows as columns instead) then thats fine, i just need to be able to get an output that gives all 3 areas/volumes from the given dimensions.

Thanks for taking some time to look at this though.
• Aug 6th 2009, 09:49 PM
CaptainBlack
Quote:

Originally Posted by deragon999
I want it like this:

function [surface_area area volume]=cylinder(height,radius)

So that the following can be typed into matlab command:

which will result in an output of:

surfacearea1 area1 volume1
surfacearea2 area2 volume2
surfacearea3 area3 volume3
etc etc etc

If the output matrix has to be aligned a different way(my examples rows as columns instead) then thats fine, i just need to be able to get an output that gives all 3 areas/volumes from the given dimensions.

Thanks for taking some time to look at this though.

Code:

```function rv=cylinder(height, radius)     % first make sure we have column vectors      sz1=size(height);   if sz1(1) ~= 1       ht=height';   else       ht=height;   end     sz1=size(radius);   if sz1(1) ~= 1       rd=radius';   else       rd=radius;   end   sarea=ht.*(2*rd*pi);   xarea=pi*rd.^2;   volume=ht.*xarea;   rv=[sarea;xarea;volume]';```
CB