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:
going to depth of matlab

Subject: going to depth of matlab

From: jasprit kour

Date: 15 Sep, 2010 06:02:07

Message: 1 of 5

i have one questions... given a matrix which may have same number in one or other row
how a matrix comparison in itself is made so that i will come to know that how many time a particular number has occurred in a matrix?
 for ex: [ 1234
              5678
              1234
              5678
              5678]
i want to get answer in which it states that 1234 has occured twice ans 5678 has occurred 3 times

Subject: going to depth of matlab

From: Grzegorz Knor

Date: 15 Sep, 2010 06:32:07

Message: 2 of 5

"jasprit kour" <jasprit.jas@gmail.com> wrote in message <i6pngu$dmc$1@fred.mathworks.com>...
> i have one questions... given a matrix which may have same number in one or other row
> how a matrix comparison in itself is made so that i will come to know that how many time a particular number has occurred in a matrix?
> for ex: [ 1234
> 5678
> 1234
> 5678
> 5678]
> i want to get answer in which it states that 1234 has occured twice ans 5678 has occurred 3 times

Your question is similiar to this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289564#772354

Subject: going to depth of matlab

From: Bruno Luong

Date: 15 Sep, 2010 07:31:06

Message: 3 of 5

There are many ways to do it. Here are three that are mots "Matlabish"

% Data
n = 20;
nd = 1;
a = 100+ceil(4*rand(n,nd))

%% HISTC, Cannot handle nd data
if nd==1
    u = unique(a(:)); % use 'ROWS' option for multidimensional data
    n = histc(a(:), u);
    fprintf('\nhist\n');
    fprintf('%f occurs %d times\n', [u n]');
end

%% better than hist since accumarray has better complexity of O(m)
% It can also handle ND data
nd = size(a,2);
[u trash loc] = unique(a, 'rows');
n = accumarray(loc,1,[size(u,1) 1]);
fprintf('\nacccumarray\n');
fprintf(['[ ' repmat('%f ',[1 nd]) '] occurs %d times\n'], [u n]');

%% A single SORT then just squeeze out the result by detecting jumps
nd = size(a,2);
[s is] = sortrows(a);
jump = diff(s,1,1);
jump(end+1,:) = Inf;
jump = find(any(jump,2)); % Use 'any' with find for ND data
u = s(jump,:);
n = diff([0; jump]);
fprintf('\nsort\n');
fprintf(['[ ' repmat('%f ',[1 nd]) '] occurs %d times\n'], [u n]');

Bruno

Subject: going to depth of matlab

From: Steve Amphlett

Date: 15 Sep, 2010 08:35:12

Message: 4 of 5

"jasprit kour" <jasprit.jas@gmail.com> wrote in message <i6pngu$dmc$1@fred.mathworks.com>...
> i have one questions... given a matrix which may have same number in one or other row
> how a matrix comparison in itself is made so that i will come to know that how many time a particular number has occurred in a matrix?
> for ex: [ 1234
> 5678
> 1234
> 5678
> 5678]
> i want to get answer in which it states that 1234 has occured twice ans 5678 has occurred 3 times

If your values are all integers, one method which may not be so obvious is to use the sparse() function to sort and count repeats.

>> x=[1234;5678;1234;5678;5678];
>> sparse(1,x,1)

ans =

   (1,1234) 2
   (1,5678) 3

Subject: going to depth of matlab

From: Bruno Luong

Date: 26 Sep, 2010 10:08:05

Message: 5 of 5

I update the HISTC method now that can work in n-column rows array:

% Data
n = 20; % number of rows
nd = 2; % number of colums of data
% Generate random matrix with values in (100:104)
A = 100+ceil(4*rand(n,nd))

% We want now to count the rows of A that are repeated

%% METHOD1: HISTC,
[u trash loc] = unique(A,'rows');
n = histc(loc,1:length(u));
fprintf('\nhist\n');
fprintf(['[ ' repmat('%f ',[1 nd]) '] occurs %d times\n'], [u n]');

%% METHOD2: ACCUMARRAY
% better than hist since accumarray has better complexity of O(m)
% It can also handle ND data
nd = size(A,2);
[u trash loc] = unique(A, 'rows');
n = accumarray(loc,1,[size(u,1) 1]);
fprintf('\nacccumarray\n');
fprintf(['[ ' repmat('%f ',[1 nd]) '] occurs %d times\n'], [u n]');


%% METHOD3: SORT
% A single SORT then just squeeze out the result by detecting jumps
nd = size(A,2);
[s is] = sortrows(A);
jump = diff(s,1,1);
jump(end+1,:) = Inf;
jump = find(any(jump,2)); % Use 'any' with find for ND data
u = s(jump,:);
n = diff([0; jump]);
fprintf('\nsort\n');
fprintf(['[ ' repmat('%f ',[1 nd]) '] occurs %d times\n'], [u n]');

% 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