# Thread: Matlab function with vector inputs and matrix outputs

1. ## 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.

2. 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

3. I want it like this:

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

So that the following can be typed into matlab command:

cylinder([height1, height2, height3...],[radius1, radius2, radius3...])

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.

4. 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:

cylinder([height1, height2, height3...],[radius1, radius2, radius3...])

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