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:
sum matrix element in different array

Subject: sum matrix element in different array

From: Saad

Date: 3 Jan, 2013 12:24:08

Message: 1 of 3

Dear All,

I would appreciate some help on this one please.

I am summing matrix elements in different arrays. Say we have 2 arrays:

Price=num2cell(1:10);
Coupon=num2cell(1:10);

Price is a cell composed of different matrices. The same for Coupon.

Now the size of the matrices inside "Price" and "Coupon" are not always equal. Of course if I run a code similar to this...

for i=1:10
    
     Price{1,i}+ coupon{1,i};
     
end

...I get an error which what you would expect because the first matrix of "Price" and "Coupon" are of different size, ....

Now I would like to run an If statement to say: if matrix sizes of Price and Coupon are similar then sum them, if they are not equal then drop that extra elements (at the end) and just sum the first elements. How can I do that please?

example to illustrate:
size(Price{1,1})=size(Coupon{1,1})= (1000,1)
size(Price{1,2})=(995,1) < size(Coupon{1,2})= (1000,1)

I would like to ignore the extra elements (at the end) of Coupon{1,2} and then sum the remaining elements (i.e. from 1:995) with Price{1,2}? How Can I do that please?

Thanks a lot

Subject: sum matrix element in different array

From: dpb

Date: 3 Jan, 2013 13:07:20

Message: 2 of 3

On 1/3/2013 6:24 AM, Saad wrote:
> Dear All,
>
> I would appreciate some help on this one please.
>
> I am summing matrix elements in different arrays. Say we have 2 arrays:
>
> Price=num2cell(1:10);
> Coupon=num2cell(1:10);
>
> Price is a cell composed of different matrices. The same for Coupon.
>
> Now the size of the matrices inside "Price" and "Coupon" are not always
> equal. Of course if I run a code similar to this...
>
> for i=1:10
> Price{1,i}+ coupon{1,i};
> end
> ...I get an error which what you would expect because the first matrix
> of "Price" and "Coupon" are of different size, ....
>
> Now I would like to run an If statement to say: if matrix sizes of Price
> and Coupon are similar then sum them, if they are not equal then drop
> that extra elements (at the end) and just sum the first elements. How
> can I do that please?
>
...

nmin=min(length(x),length(y));
s=x(1:nmin)+y(1:nmin);

Salt to suit...

--

Subject: sum matrix element in different array

From: Saad

Date: 3 Jan, 2013 18:59:08

Message: 3 of 3

dpb <none@non.net> wrote in message <kc3vq0$ro5$1@speranza.aioe.org>...
> On 1/3/2013 6:24 AM, Saad wrote:
> > Dear All,
> >
> > I would appreciate some help on this one please.
> >
> > I am summing matrix elements in different arrays. Say we have 2 arrays:
> >
> > Price=num2cell(1:10);
> > Coupon=num2cell(1:10);
> >
> > Price is a cell composed of different matrices. The same for Coupon.
> >
> > Now the size of the matrices inside "Price" and "Coupon" are not always
> > equal. Of course if I run a code similar to this...
> >
> > for i=1:10
> > Price{1,i}+ coupon{1,i};
> > end
> > ...I get an error which what you would expect because the first matrix
> > of "Price" and "Coupon" are of different size, ....
> >
> > Now I would like to run an If statement to say: if matrix sizes of Price
> > and Coupon are similar then sum them, if they are not equal then drop
> > that extra elements (at the end) and just sum the first elements. How
> > can I do that please?
> >
> ...
>
> nmin=min(length(x),length(y));
> s=x(1:nmin)+y(1:nmin);
>
> Salt to suit...
>
>

thanks a lot for your help

Regards

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