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:
addition of one column with certain condition in another colum, like sumifs of excel

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: dibyendu

Date: 8 Jul, 2012 18:20:11

Message: 1 of 9

i have a matrix like this

A=[ 1 2; 2 3; 3 4; 4 5; 5 6; 6 8; 7 9; 8 5; 9 4]
now I want to add second column, condition is that if limit=0, and interval=3 and limit=limit+interval,, or in other words, i have to sum column 2 when values of column 1, ranges like 0 to 3, 3 to 6, 6 to 9, and 9 to 12, and i want sum of corresponding values of column 2.

my solun will be like that

range-- sum
0 to 3 9
3 to 6 19
6 to 9 18


like that i have a matrix of arround 7000X2. in place of range just serial no may also be given.

this is just an example.

plz help...

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: dibyendu

Date: 8 Jul, 2012 19:05:08

Message: 2 of 9

reply plz,, urgent

"dibyendu " <paldhere@gmail.com> wrote in message <jtcj0r$rb0$1@newscl01ah.mathworks.com>...
> i have a matrix like this
>
> A=[ 1 2; 2 3; 3 4; 4 5; 5 6; 6 8; 7 9; 8 5; 9 4]
> now I want to add second column, condition is that if limit=0, and interval=3 and limit=limit+interval,, or in other words, i have to sum column 2 when values of column 1, ranges like 0 to 3, 3 to 6, 6 to 9, and 9 to 12, and i want sum of corresponding values of column 2.
>
> my solun will be like that
>
> range-- sum
> 0 to 3 9
> 3 to 6 19
> 6 to 9 18
>
>
> like that i have a matrix of arround 7000X2. in place of range just serial no may also be given.
>
> this is just an example.
>
> plz help...

Subject: addition of one column with certain condition in another colum,

From: dpb

Date: 8 Jul, 2012 20:26:24

Message: 3 of 9

...
> "dibyendu " <paldhere@gmail.com> wrote in message
> <jtcj0r$rb0$1@newscl01ah.mathworks.com>...
>> i have a matrix like this
>>
>> A=[ 1 2; 2 3; 3 4; 4 5; 5 6; 6 8; 7 9; 8 5; 9 4]
>> now I want to add second column, condition is that if limit=0, and
>> interval=3 and limit=limit+interval,, or in other words, i have to sum
>> column 2 when values of column 1, ranges like 0 to 3, 3 to 6, 6 to 9,
>> and 9 to 12, and i want sum of corresponding values of column 2.
>>
>> my solun will be like that
>>
>> range-- sum
>> 0 to 3 9
>> 3 to 6 19
>> 6 to 9 18
>>
...

Chill dood, it's a summer Sunday afternoon of 4 July week...

 >> sum(A(ismember(A(:,1),[0:3]),2))
ans =
      9
 >>

Salt to suit...

W/ newer release you may be able to use arrayfun() to purpose here; I'm
pretty sure there's a submission on the File Exchange that does the
grouping but I don't recall who submitted it nor could I seem to find
the appropriate search keyword to locate it, but you may want to give it
a go as well.

--

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: Bruno Luong

Date: 8 Jul, 2012 20:36:18

Message: 4 of 9

>> A=[ 1 2; 2 3; 3 4; 4 5; 5 6; 6 8; 7 9; 8 5; 9 4]

A =

     1 2
     2 3
     3 4
     4 5
     5 6
     6 8
     7 9
     8 5
     9 4

>> accumarray(ceil(A(:,1)/3),A(:,2))

ans =

     9
    19
    18

>>

% Bruno

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: dibyendu

Date: 9 Jul, 2012 08:13:09

Message: 5 of 9

thank you bruno.. its working so nicely and quickly..

now please tell me wht to do if i want to average the same column 2 with same interval?

thnk you once again.

dibyendu


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jtcr02$oit$1@newscl01ah.mathworks.com>...
> >> A=[ 1 2; 2 3; 3 4; 4 5; 5 6; 6 8; 7 9; 8 5; 9 4]
>
> A =
>
> 1 2
> 2 3
> 3 4
> 4 5
> 5 6
> 6 8
> 7 9
> 8 5
> 9 4
>
> >> accumarray(ceil(A(:,1)/3),A(:,2))
>
> ans =
>
> 9
> 19
> 18
>
> >>
>
> % Bruno

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: Bruno Luong

Date: 9 Jul, 2012 10:37:06

Message: 6 of 9

"dibyendu " <paldhere@gmail.com> wrote in message <jte3ql$cuv$1@newscl01ah.mathworks.com>...
> thank you bruno.. its working so nicely and quickly..
>
> now please tell me wht to do if i want to average the same column 2 with same interval?
>
>

int = ceil(A(:,1)/3);
accumarray(int ,A(:,2))./accumarray(int ,1)

% Bruno

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: dibyendu

Date: 9 Jul, 2012 11:26:07

Message: 7 of 9

thank you very much bruno,, its working great,

 I have another thred,

http://www.mathworks.com/matlabcentral/newsreader/view_thread/321658

plz check here.

 here i want to count the no of third column, with the same conditions in column 1,

i m dectating here again,,

A=[1 2;2 3;3 4;3 2;4 3;5 4;5 2;6 3;7 2;8 3]
now i want to count no of 2, 3 and 4 in column2, condition is that when column 1 ranges from 0 to 3, 3 to 6, 6 to 9, 9 to 12 like that.

answer is like that
ranges no2, no3, no4
0 to 3-- 2-----1-------1
3 to 6---1------2-------1
6 to 9-- --1-----1------0



plz help me,,,, waiting for ur reply..

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jtec8i$b5q$1@newscl01ah.mathworks.com>...
> "dibyendu " <paldhere@gmail.com> wrote in message <jte3ql$cuv$1@newscl01ah.mathworks.com>...
> > thank you bruno.. its working so nicely and quickly..
> >
> > now please tell me wht to do if i want to average the same column 2 with same interval?
> >
> >
>
> int = ceil(A(:,1)/3);
> accumarray(int ,A(:,2))./accumarray(int ,1)
>
> % Bruno

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: dibyendu

Date: 9 Jul, 2012 17:09:16

Message: 8 of 9

plz reply..waiting urgent

"dibyendu " <paldhere@gmail.com> wrote in message <jtef4f$l8t$1@newscl01ah.mathworks.com>...
> thank you very much bruno,, its working great,
>
> I have another thred,
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/321658
>
> plz check here.
>
> here i want to count the no of third column, with the same conditions in column 1,
>
> i m dectating here again,,
>
> A=[1 2;2 3;3 4;3 2;4 3;5 4;5 2;6 3;7 2;8 3]
> now i want to count no of 2, 3 and 4 in column2, condition is that when column 1 ranges from 0 to 3, 3 to 6, 6 to 9, 9 to 12 like that.
>
> answer is like that
> ranges no2, no3, no4
> 0 to 3-- 2-----1-------1
> 3 to 6---1------2-------1
> 6 to 9-- --1-----1------0
>
>
>
> plz help me,,,, waiting for ur reply..
>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jtec8i$b5q$1@newscl01ah.mathworks.com>...
> > "dibyendu " <paldhere@gmail.com> wrote in message <jte3ql$cuv$1@newscl01ah.mathworks.com>...
> > > thank you bruno.. its working so nicely and quickly..
> > >
> > > now please tell me wht to do if i want to average the same column 2 with same interval?
> > >
> > >
> >
> > int = ceil(A(:,1)/3);
> > accumarray(int ,A(:,2))./accumarray(int ,1)
> >
> > % Bruno

Subject: addition of one column with certain condition in another colum, like sumifs of excel

From: Bruno Luong

Date: 9 Jul, 2012 20:38:20

Message: 9 of 9

"dibyendu " <paldhere@gmail.com> wrote in message <jtef4f$l8t$1@newscl01ah.mathworks.com>...
> thank you very much bruno,, its working great,
>
> I have another thred,
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/321658
>
> plz check here.
>
> here i want to count the no of third column, with the same conditions in column 1,
>
> i m dectating here again,,
>
> A=[1 2;2 3;3 4;3 2;4 3;5 4;5 2;6 3;7 2;8 3]
> now i want to count no of 2, 3 and 4 in column2, condition is that when column 1 ranges from 0 to 3, 3 to 6, 6 to 9, 9 to 12 like that.

A=[1 2;2 3;3 4;3 2;4 3;5 4;5 2;6 3;7 2;8 3]

i = ceil(A(:,1)/3);
[~, j] = ismember(A(:,2),[2 3 4]);
c = accumarray([i,j],1)

How many fishing lessons do you need?

Bruno

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