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:
Count the most frequent value in each matrix row

Subject: Count the most frequent value in each matrix row

From: Ruvim

Date: 2 Jan, 2013 00:41:09

Message: 1 of 14

Hi all,

Is it possible without loops to get the most frequent value in each row of matrix?

for example I have matrix:
A = [2,2,2; 7,7,4; 0, 8, 8]
I need to get
ans = [2;7;8]

Thanks,
Ruvim

Subject: Count the most frequent value in each matrix row

From: dpb

Date: 2 Jan, 2013 00:45:37

Message: 2 of 14

On 1/1/2013 6:41 PM, Ruvim wrote:
> Hi all,
>
> Is it possible without loops to get the most frequent value in each row
> of matrix?
...

doc mode

--

Subject: Count the most frequent value in each matrix row

From: Ruvim

Date: 2 Jan, 2013 00:59:08

Message: 3 of 14

dpb <none@non.net> wrote in message <kbvvvb$ioj$1@speranza.aioe.org>...
> On 1/1/2013 6:41 PM, Ruvim wrote:
> > Hi all,
> >
> > Is it possible without loops to get the most frequent value in each row
> > of matrix?
> ...
>
> doc mode
>
> --

Amazing! Thanks.

Maybe it is even possible to do sth like:
uniqueMinorAxes = unique(minorAxes);
uniqueMinorAxes = [uniqueMinorAxes, histc(minorAxes, uniqueMinorAxes)];
if minorAxes is not vector but matrix (I need it to be done for every row as well)?
(I guess it should be returned as a structure because dimensions most probably will be different for every row).

Subject: Count the most frequent value in each matrix row

From: dpb

Date: 2 Jan, 2013 02:26:13

Message: 4 of 14

On 1/1/2013 6:59 PM, Ruvim wrote:

...
> Maybe it is even possible to do sth like:
> uniqueMinorAxes = unique(minorAxes);
> uniqueMinorAxes = [uniqueMinorAxes, histc(minorAxes, uniqueMinorAxes)];
> if minorAxes is not vector but matrix (I need it to be done for every
> row as well)? (I guess it should be returned as a structure because
> dimensions most probably will be different for every row).

unique() doesn't have the DIM optional argument to return the unique
values of each row--the 'ROWS' argument causes it to treat each row as
an entry.

--

Subject: Count the most frequent value in each matrix row

From: Ruvim

Date: 2 Jan, 2013 09:46:07

Message: 5 of 14

dpb <none@non.net> wrote in message <kc05s0$uo7$1@speranza.aioe.org>...
> On 1/1/2013 6:59 PM, Ruvim wrote:
>
> ...
> > Maybe it is even possible to do sth like:
> > uniqueMinorAxes = unique(minorAxes);
> > uniqueMinorAxes = [uniqueMinorAxes, histc(minorAxes, uniqueMinorAxes)];
> > if minorAxes is not vector but matrix (I need it to be done for every
> > row as well)? (I guess it should be returned as a structure because
> > dimensions most probably will be different for every row).
>
> unique() doesn't have the DIM optional argument to return the unique
> values of each row--the 'ROWS' argument causes it to treat each row as
> an entry.
>
> --
I know about it and just wondering maybe it is still possible to somehow rewrite this code for each row in matrices without using loop

Subject: Count the most frequent value in each matrix row

From: Bruno Luong

Date: 2 Jan, 2013 11:04:08

Message: 6 of 14

> I know about it and just wondering maybe it is still possible to somehow rewrite this code for each row in matrices without using loop

But aren't you satisfied with MODE command?

Bruno

Subject: Count the most frequent value in each matrix row

From: Bruno Luong

Date: 2 Jan, 2013 12:50:08

Message: 7 of 14

> I know about it and just wondering maybe it is still possible to somehow rewrite this code for each row in matrices without using loop

MODE is indeed using for-loop. Here is a command that does not use LOOP.

% Data
A=ceil(10*rand(10,5));

u = unique(A);
n = histc(A.',u);
[maxn i] = max(n,[],1);
modeA = u(i)

% Bruno

Subject: Count the most frequent value in each matrix row

From: Ruvim

Date: 2 Jan, 2013 12:56:05

Message: 8 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kc1478$80j$1@newscl01ah.mathworks.com>...
> > I know about it and just wondering maybe it is still possible to somehow rewrite this code for each row in matrices without using loop
>
> But aren't you satisfied with MODE command?
>
> Bruno

Well, lets say not fully. I am trying to find ellipses on the image using Hough transform and to reduce iteration count I am counting each iteration for 1000 possible ellipses. Each row is one dimentional accumulation array of minor semi-axes for possible ellipse. With mode I am not able to detect concentric ellipses as I can choose only one with biggest number of detected pixels on the edge.

Even more I can simply throw away best detected ellipse: let say 2 concentric ellipses, one small with 50% of edge pixel detected and second bigger with 40% edge pixel detected. In accumulator array 40% of larger ellipse will be > 50% of smaller ellipse.

Subject: Count the most frequent value in each matrix row

From: Bruno Luong

Date: 2 Jan, 2013 13:20:08

Message: 9 of 14

> Well, lets say not fully. I am trying to find ellipses on the image using Hough transform and to reduce iteration count I am counting each iteration for 1000 possible ellipses. Each row is one dimentional accumulation array of minor semi-axes for possible ellipse. With mode I am not able to detect concentric ellipses as I can choose only one with biggest number of detected pixels on the edge.
>
> Even more I can simply throw away best detected ellipse: let say 2 concentric ellipses, one small with 50% of edge pixel detected and second bigger with 40% edge pixel detected. In accumulator array 40% of larger ellipse will be > 50% of smaller ellipse.


You said in the first post you want MODE for each row, then using mode like this with your example gives your required output:

>> A = [2,2,2; 7,7,4; 0, 8, 8]

A =

     2 2 2
     7 7 4
     0 8 8

>> mode(A,2)

ans =

     2
     7
     8

Do you want to change the question? I must admit I read quickly through the Hough description and it is still not clear what you want.

Bruno

Subject: Count the most frequent value in each matrix row

From: Ruvim

Date: 2 Jan, 2013 15:01:07

Message: 10 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> You said in the first post you want MODE for each row, then using mode like this with your example gives your required output:
>
> >> A = [2,2,2; 7,7,4; 0, 8, 8]
>
> A =
>
> 2 2 2
> 7 7 4
> 0 8 8
>
> >> mode(A,2)
>
> ans =
>
> 2
> 7
> 8
>
> Do you want to change the question? I must admit I read quickly through the Hough description and it is still not clear what you want.
>
> Bruno

Yes, I want to extend initial question:
with [maxVal freq] = mode(A, 2) I can get
[2,3; 7,2; 8,2]
and what I now want to get is something like
2,3;
[7,2], [4,1]
[8,2], [0,1]
stored in some structure

but if you say that MODE use loops so I guess it will be the same as iterating over each row and doing: hist(row, unique(row));

Subject: Count the most frequent value in each matrix row

From: dpb

Date: 2 Jan, 2013 15:44:07

Message: 11 of 14

On 1/2/2013 9:01 AM, Ruvim wrote:
...

> Yes, I want to extend initial question:
> with [maxVal freq] = mode(A, 2) I can get
> [2,3; 7,2; 8,2]
> and what I now want to get is something like
> 2,3;
> [7,2], [4,1]
> [8,2], [0,1]
> stored in some structure
>
> but if you say that MODE use loops so I guess it will be the same as
> iterating over each row and doing: hist(row, unique(row));

Basically, yes, as far as loops go. Your request really is hist-like
more than just mode() but look at second example in mode() help for 12b
(online if it isn't an example of using histc in grouped data for mode
in your release). It's a similar usage if not exactly...

type mode.m

and you can see what TMW does internally in mode.m (I'm surprised it
hasn't been mex'ed or at least p'ed as has much else in 12b, but it's
still a m-file)

--


--

Subject: Count the most frequent value in each matrix row

From: Bruno Luong

Date: 2 Jan, 2013 16:07:05

Message: 12 of 14

"Ruvim" wrote in message <kc1i3j$spd$1@newscl01ah.mathworks.com>...

> Yes, I want to extend initial question:
> with [maxVal freq] = mode(A, 2) I can get
> [2,3; 7,2; 8,2]
> and what I now want to get is something like
> 2,3;
> [7,2], [4,1]
> [8,2], [0,1]
> stored in some structure

Modification of my pots #7:

% Data
A = [2,2,2; 7,7,4; 0, 8, 8]

% Engine
u = unique(A);
n = histc(A.',u);
[i j n] = find(n);
k = (1:length(j))';
x = accumarray(j,k,[],@(x) {[u(i(x)) n(x)]});

% Check the result
x{:}

% Bruno

Subject: Count the most frequent value in each matrix row

From: Bruno Luong

Date: 2 Jan, 2013 18:53:08

Message: 13 of 14

dpb <none@non.net> wrote in message <kc1kjv$6ka$1@speranza.aioe.org>...
>
> and you can see what TMW does internally in mode.m (I'm surprised it
> hasn't been mex'ed or at least p'ed as has much else in 12b, but it's
> still a m-file)
>

And it is not a speed deamon.

Bruno

Subject: Count the most frequent value in each matrix row

From: Ruvim

Date: 5 Jan, 2013 13:51:08

Message: 14 of 14

> Modification of my pots #7:
>
> % Data
> A = [2,2,2; 7,7,4; 0, 8, 8]
>
> % Engine
> u = unique(A);
> n = histc(A.',u);
> [i j n] = find(n);
> k = (1:length(j))';
> x = accumarray(j,k,[],@(x) {[u(i(x)) n(x)]});
>
> % Check the result
> x{:}
>
> % Bruno


Thanks a lot, it works!

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