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:
why it doesn't increment ?

Subject: why it doesn't increment ?

From: Patrick Diviacco

Date: 30 Oct, 2009 18:32:04

Message: 1 of 6

Hi,


my original matrix is


histogram =

     0 0 0
     0 0 0
     0 0 0



I want to have this matrix as result:

histogram =

     3 0 0
     1 0 0
     0 0 0



This is the code:

histogram([1 1 1 2],[1 1 1 1]) = histogram([1 1 1 2],[1 1 1 1]) + 1;


but it doesn't work, it seems that same indexes are ignored.

I can only increment by 1 using the 2 vector indexes.

thanks

Subject: why it doesn't increment ?

From: Nathan

Date: 30 Oct, 2009 18:55:05

Message: 2 of 6

On Oct 30, 11:32 am, "Patrick Diviacco" <patrick.divia...@gmail.com>
wrote:
> Hi,
>
> my original matrix is
>
> histogram =
>
>      0     0     0
>      0     0     0
>      0     0     0
>
> I want to have this matrix as result:
>
> histogram =
>
>      3     0     0
>      1     0     0
>      0     0     0
>
> This is the code:
>
> histogram([1 1 1 2],[1 1 1 1]) = histogram([1 1 1 2],[1 1 1 1]) + 1;
>
> but it doesn't work, it seems that same indexes are ignored.
>
> I can only increment by 1 using the 2 vector indexes.
>
> thanks

Well, one way:
r = [1 1 1 2]; c = [1 1 1 1];
for x = 1:length(r)
histogram(r(x),c(x)) = histogram(r(x),c(x)) + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
histogram =
     3 0 0
     1 0 0
     0 0 0

I couldn't think of a vectorized way to do it, however.

-Nathan

Subject: why it doesn't increment ?

From: the cyclist

Date: 30 Oct, 2009 19:12:03

Message: 3 of 6

"Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hcfbf3$6ia$1@fred.mathworks.com>...
> Hi,
>
>
> my original matrix is
>
>
> histogram =
>
> 0 0 0
> 0 0 0
> 0 0 0
>
>
>
> I want to have this matrix as result:
>
> histogram =
>
> 3 0 0
> 1 0 0
> 0 0 0
>
>
>
> This is the code:
>
> histogram([1 1 1 2],[1 1 1 1]) = histogram([1 1 1 2],[1 1 1 1]) + 1;
>
>
> but it doesn't work, it seems that same indexes are ignored.
>
> I can only increment by 1 using the 2 vector indexes.
>
> thanks

I think I would do this by looping, to avoid obfuscation, but there IS a vectorized way to to this:

>> histogram = zeros(3,3);
>> sub1 = [1 1 1 2]';
>> sub2 = [1 1 1 1]';
>> incrementAmount = accumarray([sub1,sub2],1);
>> uniqueRows = unique([sub1,sub2],'rows');
>> linearIndex = sub2ind(size(histogram),uniqueRows(:,1),uniqueRows(:,2));
>> histogram(linearIndex) = histogram(linearIndex) + incrementAmount;

the cyclist

Subject: why it doesn't increment ?

From: Loren Shure

Date: 30 Oct, 2009 19:17:13

Message: 4 of 6

In article <hcfbf3$6ia$1@fred.mathworks.com>, patrick.diviacco@gmail.com
says...
> Hi,
>
>
> my original matrix is
>
>
> histogram =
>
> 0 0 0
> 0 0 0
> 0 0 0
>
>
>
> I want to have this matrix as result:
>
> histogram =
>
> 3 0 0
> 1 0 0
> 0 0 0
>
>
>
> This is the code:
>
> histogram([1 1 1 2],[1 1 1 1]) = histogram([1 1 1 2],[1 1 1 1]) + 1;
>
>
> but it doesn't work, it seems that same indexes are ignored.
>
> I can only increment by 1 using the 2 vector indexes.
>
> thanks
>


Check out accumarray?
--
Loren
http://blogs.mathworks.com/loren

Subject: why it doesn't increment ?

From: the cyclist

Date: 30 Oct, 2009 19:24:02

Message: 5 of 6

"the cyclist" <thecyclist@gmail.com> wrote in message <hcfdq3$1vd$1@fred.mathworks.com>...
> "Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hcfbf3$6ia$1@fred.mathworks.com>...
> > Hi,
> >
> >
> > my original matrix is
> >
> >
> > histogram =
> >
> > 0 0 0
> > 0 0 0
> > 0 0 0
> >
> >
> >
> > I want to have this matrix as result:
> >
> > histogram =
> >
> > 3 0 0
> > 1 0 0
> > 0 0 0
> >
> >
> >
> > This is the code:
> >
> > histogram([1 1 1 2],[1 1 1 1]) = histogram([1 1 1 2],[1 1 1 1]) + 1;
> >
> >
> > but it doesn't work, it seems that same indexes are ignored.
> >
> > I can only increment by 1 using the 2 vector indexes.
> >
> > thanks
>
> I think I would do this by looping, to avoid obfuscation, but there IS a vectorized way to to this:
>
> >> histogram = zeros(3,3);
> >> sub1 = [1 1 1 2]';
> >> sub2 = [1 1 1 1]';
> >> incrementAmount = accumarray([sub1,sub2],1);
> >> uniqueRows = unique([sub1,sub2],'rows');
> >> linearIndex = sub2ind(size(histogram),uniqueRows(:,1),uniqueRows(:,2));
> >> histogram(linearIndex) = histogram(linearIndex) + incrementAmount;
>
> the cyclist

Note that this happens to work for your particular case, but does not generalize well. I think my use of accumarray isn't quite right.

Subject: why it doesn't increment ?

From: Matt Fig

Date: 30 Oct, 2009 20:52:01

Message: 6 of 6

Does this do the trick generally?

% Data
histogram = round(rand(3)*8)
r = [1 1 1 2];
c = [1 1 1 1];

% Engine
I = r + (c-1)*size(histogram,1);
UI = unique(I);
histogram(UI) = histogram(UI) + 1*histc(I,UI);

Tags for this Thread

No tags are associated with 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