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:
Feature Request, aggregate in unique function

Subject: Feature Request, aggregate in unique function

From: William

Date: 12 May, 2012 07:51:12

Message: 1 of 5

One of the values returned by the function 'unique' is 'ia', an n-by-1 matrix of indexes to the original array. Each index in 'ia' is the 'first' or 'last' value encountered for each unique value found.

How feasible would it be for MathWorks to add a new feature to the 'unique' function: a new occurrence parameter (i.e. 'all') that would return 'ia' as a n-by-1 cell array corresponding to each unique value, and each cell contains an n-by-1 matrix of indexes to the original array?

Once that cell of matrices is built, it becomes almost trivial to count, add, average, etc. per group. The catch is that A is being scanned twice to build this. The function below (unique1) would be greatly simplified if 'unique' didn't discard everything except the 'first' or 'last' occurrence when it originally cycled through the array.

Below are the sample functions. 'unique1' builds upon 'unique' and 'unique1_test' provides sample data and displays the result.

%unique1.m
function [C, ia, ic] = unique1(A, occurrence)
  A0 = A(:);
  [C, ia, ic] = unique(A0, 'last');
  
  %revisit A, aggregate instead of 'last'
  if strcmpi(occurrence, 'all')
    steps = transpose(1:numel(A0)); %(n x 1 matrix)
    ia = cell(size(ia, 1), 1); %Convert ia to cell array of groups
    for i = 1:size(ic, 1)
      j = ic(i);
      k = size(ia{j}, 1);
      if k == 0
        ia{j} = []; %Create column vector within group cell of ia
      end
      ia{j}(k + 1, 1) = steps(i); %Populate column vector
    end
  end
end

%unique1_test.m
function unique1_test()
  clear; clc;
  users = {'Smith, A';'Jones, I';'Jones, I';'Doe, J';'Miller, F';'Doe, J';'Smith, A';'Smith, A';'Smith, A';'Jones, I'}
  fruits = {'Apple' ;'Banana' ;'Apple' ;'Pear' ;'Orange' ;'Apple' ;'Kiwi' ;'Cherry' ;'Banana' ;'Grapefruit'}
  [C, ia, ic] = unique1(users, 'all');
 
  %Display Results
  fprintf('Output:\n\n');
  for i = 1:numel(C)
    fprintf('%s\n----------\n', C{i});
    details = ia{i};
    detailfruits = fruits(details);
    detailfruits = sort(detailfruits);
    for j = 1:numel(details)
      fprintf(' %s\n', detailfruits{j});
    end
    fprintf('----------\nTotal: %d\n\n', numel(details));
  end
end

Thanks for your time

Will

Subject: Feature Request, aggregate in unique function

From: Bruno Luong

Date: 12 May, 2012 08:05:24

Message: 2 of 5


> How feasible would it be for MathWorks to add a new feature to the 'unique' function: a new occurrence parameter (i.e. 'all') that would return 'ia' as a n-by-1 cell array corresponding to each unique value, and each cell contains an n-by-1 matrix of indexes to the original array?

It's not hard to get with stock functions

[u, ia, ic] = unique(c)
ia = accumarray(ic,(1:length(ic))',[],@(x) {sort(x)})

% Bruno

Subject: Feature Request, aggregate in unique function

From: james bejon

Date: 12 May, 2012 08:37:05

Message: 3 of 5

On the subject of feature requests, it's often struck me that it would be a nice behaviour if accumarray had a value for "subs" causing the corresponding element of "vals" to be ignored altogether. Thus, accumarray ([1; NaN; 1], [2; 3; 4]) would return [6]. I don't know if this would be useful to anyone else...

Subject: Feature Request, aggregate in unique function

From: Bruno Luong

Date: 12 May, 2012 09:48:08

Message: 4 of 5

I woud like to add an option in accumarray so that the data arrived in the function handle in the same order than in their linear index, so that:

a = accumarray(ic,(1:length(ic))',[],@(x) {sort(x)})

Can be achieved with a new syntax:

a = accumarray(ic,(1:length(ic))',[],@(x) {x}, 'stable')

% Bruno

Subject: Feature Request, aggregate in unique function

From: William

Date: 12 May, 2012 16:58:29

Message: 5 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jol5k4$b1v$1@newscl01ah.mathworks.com>...
>
> > How feasible would it be for MathWorks to add a new feature to the 'unique' function: a new occurrence parameter (i.e. 'all') that would return 'ia' as a n-by-1 cell array corresponding to each unique value, and each cell contains an n-by-1 matrix of indexes to the original array?
>
> It's not hard to get with stock functions
>
> [u, ia, ic] = unique(c)
> ia = accumarray(ic,(1:length(ic))',[],@(x) {sort(x)})
>
> % Bruno

That's what I was looking for. It appears ideal for exploring element groups.
http://blogs.mathworks.com/loren/2008/02/20/under-appreciated-accumarray/

Thanks

Will

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