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:
reassigning values of elements in matrix

Subject: reassigning values of elements in matrix

From: Namo Namo

Date: 16 Jul, 2010 04:57:03

Message: 1 of 5

I need to re-assign the elements in a matrix to another set of values. The simplest, say, is
to assign by order stats. For example, if I have

a = [ 1 3 4 6 3 1 4 ];
 
By order stats, I only have 4 unique elements 1 3 4 6. I would like to re-number the elements to 1 2 3 4. Currently I am using unique:

 [d, I, J] = unique( a(:) );
  new = 1:length(d);
  a2 = new(J);
 
This is good enough, but not so intuitive? Any other suggestions?
 

Subject: reassigning values of elements in matrix

From: Bruno Luong

Date: 16 Jul, 2010 06:48:07

Message: 2 of 5

"Namo Namo" <wynamo@yahoo.com> wrote in message <i1ooqv$31l$1@fred.mathworks.com>...
> I need to re-assign the elements in a matrix to another set of values. The simplest, say, is
> to assign by order stats. For example, if I have
>
> a = [ 1 3 4 6 3 1 4 ];
>
> By order stats, I only have 4 unique elements 1 3 4 6. I would like to re-number the elements to 1 2 3 4. Currently I am using unique:
>
> [d, I, J] = unique( a(:) );
> new = 1:length(d);
> a2 = new(J);
>

[d, I, a2] = unique( a)

Bruno

Subject: reassigning values of elements in matrix

From: us

Date: 16 Jul, 2010 08:22:09

Message: 3 of 5

"Namo Namo" <wynamo@yahoo.com> wrote in message <i1ooqv$31l$1@fred.mathworks.com>...
> I need to re-assign the elements in a matrix to another set of values. The simplest, say, is
> to assign by order stats. For example, if I have
>
> a = [ 1 3 4 6 3 1 4 ];
>
> By order stats, I only have 4 unique elements 1 3 4 6. I would like to re-number the elements to 1 2 3 4. Currently I am using unique:
>
> [d, I, J] = unique( a(:) );
> new = 1:length(d);
> a2 = new(J);
>
> This is good enough, but not so intuitive? Any other suggestions?

hmm...
isn't J already what you're looking for(?)...

     a=10*[1,2,3,1,2,3];
     [au,ax,ax]=unique(a);
     an=1:numel(au);
     an=an(ax);
     isequal(ax,an)
% ans = 1

us

Subject: reassigning values of elements in matrix

From: Namo Namo

Date: 16 Jul, 2010 18:23:04

Message: 4 of 5

Thanks for the reply. Yes, the second output of unique is already what I want.

I actually had a more general re-assignment problem to deal with, in which not just re-assign to the order stats. But now I find out I can just use table lookup or interp1. Say if I want to convert 3 1 4 to 1 2 3, and need to apply this conversion to a large matrix X, I can just do

interp1([3 1 4], [1 2 3], X)

and it replaces 3 1 4 in X to 1 2 3.

Subject: reassigning values of elements in matrix

From: Bruno Luong

Date: 16 Jul, 2010 18:50:20

Message: 5 of 5

"Namo Namo" <wynamo@yahoo.com> wrote in message <i1q828$sd0$1@fred.mathworks.com>...
> Thanks for the reply. Yes, the second output of unique is already what I want.
>
> I actually had a more general re-assignment problem to deal with, in which not just re-assign to the order stats. But now I find out I can just use table lookup or interp1. Say if I want to convert 3 1 4 to 1 2 3, and need to apply this conversion to a large matrix X, I can just do
>
> interp1([3 1 4], [1 2 3], X)
>
> and it replaces 3 1 4 in X to 1 2 3.

INTERP1 is slows (a hammer). The look up table is better coded simply by ... a look up table.

% Note: Use third output UNIQUE if values are not integer
table([3 1 4])= 1:3;
Y = table(X)

% or by ISMEMBER

 [~, Y]=ismember(X,[3 1 4])

% 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