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:
Can Somebody Please Help Me With This Problem?

Subject: Can Somebody Please Help Me With This Problem?

From: jat_r

Date: 8 Apr, 2008 14:53:26

Message: 1 of 17

I am currently struggling with a problem and I was wondering if somebody may
have the solution or a similar problem with a solution so I could work of it?
Any help would be appreciated as I am dreadful when it comes to MATLAB. The
problem is:

Write a MATLAB function which computes the cumulative product of the elements
in a vector. The cumulative product of the jth element of the vector x, xj,
is defined by

pj = (x1)(x2) ... (xj)

for j = 1:length of the vector x. Create 2 different versions of this
function.

a. One that uses two for-loops to explicitly carry out the calculations,
element by element. An "inner" loop should accumulate the product and an
"outer" loop should move through the elements of the vector p.

b. One that uses the built-in function prod to replace the inner loop.

Subject: Can Somebody Please Help Me With This Problem?

From: French Caro

Date: 8 Apr, 2008 15:02:04

Message: 2 of 17

Hi,

Even if MATLAB seems too difficult for you, can't you just
write the algorithms ?
It should be a good way for beggining.
And then you can search the MATLAB help which is very helpfull.


"jat_r" <u42789@uwe> wrote in message <8260282386d16@uwe>...
> I am currently struggling with a problem and I was
wondering if somebody may
> have the solution or a similar problem with a solution so
I could work of it?
> Any help would be appreciated as I am dreadful when it
comes to MATLAB. The
> problem is:
>
> Write a MATLAB function which computes the cumulative
product of the elements
> in a vector. The cumulative product of the jth element of
the vector x, xj,
> is defined by
>
> pj = (x1)(x2) ... (xj)
>
> for j = 1:length of the vector x. Create 2 different
versions of this
> function.
>
> a. One that uses two for-loops to explicitly carry out the
calculations,
> element by element. An "inner" loop should accumulate the
product and an
> "outer" loop should move through the elements of the vector p.
>
> b. One that uses the built-in function prod to replace the
inner loop.
>

Subject: Can Somebody Please Help Me With This Problem?

From: Steve Amphlett

Date: 8 Apr, 2008 15:10:04

Message: 3 of 17

"French Caro " <caro95470@nospam.free.fr> wrote in message
<ftg1dc$9i2$1@fred.mathworks.com>...
> Hi,
>
> Even if MATLAB seems too difficult for you, can't you just
> write the algorithms ?
> It should be a good way for beggining.
> And then you can search the MATLAB help which is very
helpfull.
>
>
> "jat_r" <u42789@uwe> wrote in message
<8260282386d16@uwe>...
> > I am currently struggling with a problem and I was
> wondering if somebody may
> > have the solution or a similar problem with a solution
so
> I could work of it?
> > Any help would be appreciated as I am dreadful when it
> comes to MATLAB. The
> > problem is:
> >
> > Write a MATLAB function which computes the cumulative
> product of the elements
> > in a vector. The cumulative product of the jth element
of
> the vector x, xj,
> > is defined by
> >
> > pj = (x1)(x2) ... (xj)
> >
> > for j = 1:length of the vector x. Create 2 different
> versions of this
> > function.
> >
> > a. One that uses two for-loops to explicitly carry out
the
> calculations,
> > element by element. An "inner" loop should accumulate
the
> product and an
> > "outer" loop should move through the elements of the
vector p.
> >
> > b. One that uses the built-in function prod to replace
the
> inner loop.
> >
>



"beggining"? "Begging" more like.

Subject: Can Somebody Please Help Me With This Problem?

From: jat_r

Date: 8 Apr, 2008 15:10:47

Message: 4 of 17

ill have a go at writing the algorithms, could you tell me where the matlab
help is? do you know of any similar problems to this? thanks

Subject: Can Somebody Please Help Me With This Problem?

From: jat_r

Date: 8 Apr, 2008 15:17:17

Message: 5 of 17

yes i am begging but thats because i'm completely clueless and im just
looking for some help or some direction. i don't even understand what the
question is asking so i would appreciate any help. if you can't help or don't
want to then fair enough but thank you anway.

Subject: Can Somebody Please Help Me With This Problem?

From: French Caro

Date: 8 Apr, 2008 15:20:10

Message: 6 of 17

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote
in message <ftg1sc$jfp$1@fred.mathworks.com>...
>
> "beggining"? "Begging" more like.
>

Is it a joke or did I make a mistake ?
Sorry but as my name says English is not my mother tongue...

Subject: Can Somebody Please Help Me With This Problem?

From: David

Date: 8 Apr, 2008 15:53:02

Message: 7 of 17

"jat_r" <u42789@uwe> wrote in message
<82605d61fa256@uwe>...
> yes i am begging but thats because i'm completely
clueless and im just
> looking for some help or some direction. i don't even
understand what the
> question is asking so i would appreciate any help. if
you can't help or don't
> want to then fair enough but thank you anway.
>

the question gives you the algorithms:

<quote>
a. One that uses two for-loops to explicitly carry out the
calculations,
element by element. An "inner" loop should accumulate the
product and an
"outer" loop should move through the elements of the
vector p.

b. One that uses the built-in function prod to replace the
inner loop.
</quote>

so for a. you use nested for loops and for b you use one
loop with the prod function... if this is an example of
today's homework questions i wish i were back in school.

so try 'doc for' and 'doc prod' if there aren't enough
examples there then go find the ta.

Subject: Can Somebody Please Help Me With This Problem?

From: Dave

Date: 8 Apr, 2008 16:05:10

Message: 8 of 17

jat_r wrote:
> I am currently struggling with a problem and I was wondering if somebody may
> have the solution or a similar problem with a solution so I could work of it?
> Any help would be appreciated as I am dreadful when it comes to MATLAB. The
> problem is:
>
> Write a MATLAB function which computes the cumulative product of the elements
> in a vector. The cumulative product of the jth element of the vector x, xj,
> is defined by
>
> pj = (x1)(x2) ... (xj)
>
> for j = 1:length of the vector x. Create 2 different versions of this
> function.
>
> a. One that uses two for-loops to explicitly carry out the calculations,
> element by element. An "inner" loop should accumulate the product and an
> "outer" loop should move through the elements of the vector p.
>
> b. One that uses the built-in function prod to replace the inner loop.
>


You forgot to post the email address of your lecturer, so answers can be
sent directly to him/her.

Why not make a start and show people what you have?

Also worthwhile is to put a sensible title.

Subject: Can Somebody Please Help Me With This Problem?

From: jat_r

Date: 8 Apr, 2008 16:12:24

Message: 9 of 17

ok, this is my first time on this site so i will know for next time, i am
just clueless, but the previous post has atleast given me some direction but
any more help would be appreciated. just desperate! ill pay one of you if you
do it for me lol!

Subject: Can Somebody Please Help Me With This Problem?

From: jat_r

Date: 8 Apr, 2008 16:12:45

Message: 10 of 17

ok, this is my first time on this site so i will know for next time, i am
just clueless, but the previous post has atleast given me some direction but
any more help would be appreciated. just desperate!

Subject: Can Somebody Please Help Me With This Problem?

From: Peter Boettcher

Date: 8 Apr, 2008 16:31:15

Message: 11 of 17

"jat_r" <u42789@uwe> writes:

> ok, this is my first time on this site so i will know for next time, i am
> just clueless, but the previous post has atleast given me some direction but
> any more help would be appreciated. just desperate! ill pay one of you if you
> do it for me lol!

It's not too late. Think about the problem, write down the two for
loops that you know you are supposed to use, and post what you come up
with. Along with your question about what specific piece you can't
figure out next.

Here's a start:

% Generate some test data:
x = [2 3 4 5 6];

To multiply two numbers, say, x(1) and x(2), you'd use the * symbol. So
x(1)*x(2). To multiply three numbers, you could do x(1)*x(2)*x(3). But
that will be hard to put in a loop. How do you think you could write
multiplications so that each time through the loop, you'd multiply
exactly two numbers?

-Peter

Subject: Can Somebody Please Help Me With This Problem?

From: jat_r

Date: 8 Apr, 2008 17:14:01

Message: 12 of 17

i dont understand the question and even if i did i dont think i could put it
into matlab language.
you said generate some data but how much data is required?
and you said to mutliply only two values, so could the loop just mulitply two
of the values and then another different two until they have all been done? i
must sound like such an idiot!

Subject: Can Somebody Please Help Me With This Problem?

From: David

Date: 8 Apr, 2008 17:16:03

Message: 13 of 17

"jat_r" <u42789@uwe> wrote in message
<8260d8a62d340@uwe>...
> ok, this is my first time on this site so i will know
for next time, i am
> just clueless, but the previous post has atleast given
me some direction but
> any more help would be appreciated. just desperate! ill
pay one of you if you
> do it for me lol!
>


most of us have full time jobs already... try checking out
www.rentacoder.com for programmers willing to do homework
for hire.

Subject: Can Somebody Please Help Me With This Problem?

From: Peter Boettcher

Date: 8 Apr, 2008 17:24:08

Message: 14 of 17

"jat_r" <u42789@uwe> writes:

> i dont understand the question and even if i did i dont think i could put it
> into matlab language.

OK, here's an example. The "Generate data" line IS generating some test
data.

Here it is again.

x = [2 3 4 5 6];

OK the outer loop. This tracks how many numbers to multiply.

product of 2
product of 2 3
product of 2 3 4
product of 2 3 4 5
product of 2 3 4 5 6

OK now the inner loop, which just computes the products of the number
lists above.

(1)*2 = 2
2*3 = 6
2*3*4 = 24
etc.

That's your answer. 2 6 24 ...

> and you said to mutliply only two values, so could the loop just
> mulitply two of the values and then another different two until they
> have all been done?

Right. Store the product until now. Start with 1. Then multiply the
first number, and store the product. Then multiply the next number, and
store. Etc, etc.

If your class notes do not discuss basic MATLAB concepts like vectors
and indexing, you might want to take some hours and work through the
entire "Getting Started" section of the MATLAB documentation.


-Peter

Subject: Can Somebody Please Help Me With This Problem?

From: jat_r via MathKB.com

Date: 9 Apr, 2008 10:46:15

Message: 15 of 17

This is the solution i have come up with, can somebody please check it for me?
Thank u!

function result=cumulativeP(a);
%a vector of elements
n=length(a);
result=0;
for i=1:n
product=1;
for j=1:i
product=product*a(j);
end;
result=result+product;
end;

function result=cumulativeP1(a);
%a vector of elements
n=length(a);
result=0;
product=0;
for i=1:n
product=prod(a(1:i));
result=result+product;
end;


>> A=(1:3)

A =

1 2 3

>> cumulativeP(A)

ans =

9

>> cumulativeP1(A)

ans =

9

>> sum(cumprod(A))

ans =

9

jat_r wrote:
>I am currently struggling with a problem and I was wondering if somebody may
>have the solution or a similar problem with a solution so I could work of it?
>Any help would be appreciated as I am dreadful when it comes to MATLAB. The
>problem is:
>
>Write a MATLAB function which computes the cumulative product of the elements
>in a vector. The cumulative product of the jth element of the vector x, xj,
>is defined by
>
>pj = (x1)(x2) ... (xj)
>
>for j = 1:length of the vector x. Create 2 different versions of this
>function.
>
>a. One that uses two for-loops to explicitly carry out the calculations,
>element by element. An "inner" loop should accumulate the product and an
>"outer" loop should move through the elements of the vector p.
>
>b. One that uses the built-in function prod to replace the inner loop.

--
Message posted via MathKB.com
http://www.mathkb.com/Uwe/Forums.aspx/matlab/200804/1

Subject: Can Somebody Please Help Me With This Problem?

From: French Caro

Date: 9 Apr, 2008 18:06:04

Message: 16 of 17

Hey that's better but...
As I understand the problem I think you don't have to do the
sum.
For the first question I will do something like this :

x=1:3;
for i=1:length(x)
   disp('Cumulative product of :');
   disp(x(1:i));
   p=x(1);
   for i=2:length(x(1:i))
      p=p*x(i);
   end
   disp(p);
end

I let you the second one but you're in the good way.

Subject: Can Somebody Please Help Me With This Problem?

From: Tim Davis

Date: 9 Apr, 2008 22:18:01

Message: 17 of 17

"French Caro " <caro95470@nospam.free.fr> wrote in message
<ftg2fa$sf0$1@fred.mathworks.com>...
> "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote
> in message <ftg1sc$jfp$1@fred.mathworks.com>...
> >
> > "beggining"? "Begging" more like.
> >
>
> Is it a joke or did I make a mistake ?
> Sorry but as my name says English is not my mother tongue...

No, you did not make a mistake. Nor did you make a joke.
Steve just made a joke - but he wasn't making fun of your
English.

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