Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

recursion multiplication logic question tia sal22

0 views
Skip to first unread message

Rick T

unread,
Nov 18, 2009, 3:45:19 PM11/18/09
to
recursion multiplication logic question

Greetings All

I have an array
[1 2
3 4
5 6]

I'm trying to get entire columns to multiple together using recursion
but I'm having trouble with the logic.

Example: the array above should come back with
[15 48] which is (1*3*5) and (2*4*6)

I attempted to do this using a matlab for loop but no luck as of yet.
Does anyone know the correct logic?

xarray=[1 2;3 4;5 6];
multiply=0;
[rowSize,colSize]=size(xarray);
for cc=1:colSize,
for rr=1:rowSize,
multiply(1,cc)=multiply(rr,cc)*xarray(rr+1,cc);
end;
end;

tia sal22

Steven Lord

unread,
Nov 18, 2009, 10:45:19 PM11/18/09
to

"Rick T" <ratu...@gmail.com> wrote in message
news:7cc347e3-39ec-40c3...@u8g2000prd.googlegroups.com...

> recursion multiplication logic question
>
> Greetings All
>
> I have an array
> [1 2
> 3 4
> 5 6]
>
> I'm trying to get entire columns to multiple together using recursion
> but I'm having trouble with the logic.
>
> Example: the array above should come back with
> [15 48] which is (1*3*5) and (2*4*6)

Assuming this isn't homework, you don't need to use recursion. Just use the
PROD function with the dim input argument to specify that you want to
multiply down the columns.

> I attempted to do this using a matlab for loop but no luck as of yet.

You could also do this with one FOR loop, appropriate indexing to extract
rows of your matrix, and the .* operator.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


0 new messages