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:
working with matrix

Subject: working with matrix

From: anil chandy

Date: 16 Feb, 2009 09:27:02

Message: 1 of 13

a=1 2 3 4
    3 4 5 7
    2 4 5 4
b=[4 4 4 8 8 6 6 9]

c=[1 3 5 6 7 2 8 4]
 here in b the same elements are grouped.. and c denotes the indices of the repeating elements in the original matrix
d=[4 6 4 9 4 8 8 6]
 is there any method to take the elements of c as indices and substitute the values of a in that place ie e= 1 3 1 4 1 2 2 3
                                                              2 5 2 7 2 4 4 5
                                                              3 5 3 4 3 4 4 5

Subject: working with matrix

From: anil chandy

Date: 16 Feb, 2009 11:05:03

Message: 2 of 13

"anil chandy" <anilvchandy@gmail.com> wrote in message <gnbbh6$946$1@fred.mathworks.com>...
> a=1 2 3 4
> 3 4 5 7
> 2 4 5 4
> b=[4 4 4 8 8 6 6 9]
>
> c=[1 3 5 6 7 2 8 4]
> here in b the same elements are grouped.. and c denotes the indices of the repeating elements in the original matrix
> d=[4 6 4 9 4 8 8 6]
> is there any method to take the elements of c as indices and substitute the values of a in that place ie e= 1 3 1 4 1 2 2 3
> 2 5 2 7 2 4 4 5
> 3 5 3 4 3 4 4 5

Subject: working with matrix

From: us

Date: 16 Feb, 2009 11:45:03

Message: 3 of 13

"anil chandy"
> a=1 2 3 4
> 3 4 5 7
> 2 4 5 4
> b=[4 4 4 8 8 6 6 9]
> c=[1 3 5 6 7 2 8 4]
> here in b the same elements are grouped.. and c denotes the indices of the repeating elements in the original matrix
> d=[4 6 4 9 4 8 8 6]
> is there any method to take the elements of c as indices and substitute the values of a in that place ie e= 1 3 1 4 1 2 2 3
> 2 5 2 7 2 4 4 5
> 3 5 3 4 3 4 4 5

your example is not clear...

in general, thought, creating an array based on a
- template of values -> your a
- a list of indices -> your c
can be achieved by

% the list of indices into...
     lst=[
           3,2,1,16
          16,16,16,16
     ];
% ...a template of values
     v=magic(4)
%{
% v =
    16 2 3 13
      5 11 10 8
      9 7 6 12
      4 14 15 1
%}
     r=v(lst)
%{
% r =
     9 5 16 1 % <- note: same size as LST
     1 1 1 1
%}

us

Subject: working with matrix

From: us

Date: 16 Feb, 2009 11:55:03

Message: 4 of 13

"us"
> in general, thought, creating an array based on a

although i gave some thought to this problem, this should have read
..., though,...

just a thought...
us

Subject: working with matrix

From: Roger Stafford

Date: 17 Feb, 2009 00:59:01

Message: 5 of 13

"anil chandy" <anilvchandy@gmail.com> wrote in message <gnbbh6$946$1@fred.mathworks.com>...
> a=1 2 3 4
> 3 4 5 7
> 2 4 5 4
> b=[4 4 4 8 8 6 6 9]
>
> c=[1 3 5 6 7 2 8 4]
> here in b the same elements are grouped.. and c denotes the indices of the repeating elements in the original matrix
> d=[4 6 4 9 4 8 8 6]
> is there any method to take the elements of c as indices and substitute the values of a in that place ie e= 1 3 1 4 1 2 2 3
> 2 5 2 7 2 4 4 5
> 3 5 3 4 3 4 4 5

  It is difficult to understand what you are saying, Anil. You explanation is very unclear. However, I found a way of producing arrays d and e from a, b, and c, provided one correction is made to e. Is this the kind of thing you wanted to do? I see no way of deducing either b or c from a. The following depends on c being a valid permutation of 1:8.

 d = zeros(size(c));
 d(c) = b;
 p = cumsum([1,diff(b)~=0]);
 p(c) = p;
 e = a(:,p);

The d and e produced are:

 d = [4 6 4 9 4 8 8 6]

 e = [1 3 1 4 1 2 2 3
      3 5 3 7 3 4 4 5
      2 5 2 4 2 4 4 5]

You seem to have the 2's and 3's in the second two rows of e interchanged. Is that a mistake?

Roger Stafford

Subject: working with matrix

From: anil chandy

Date: 17 Feb, 2009 05:29:02

Message: 6 of 13

its a mistake.. wat i actually meant was i ve a matrix A=[ 4 6 4 9 4 8 8 6 ]
and another matrix B=[4 4 4 6 6 8 8 9] which is formed by grouping the similar terms in D .. and another matrix c=[1 3 5 2 8 6 7 4] which denotes the indices of 4 6 8 and 9 in the matrix D..
i got one more matrix [ 1 2 3 4
                                  3 4 5 7
                                  2 4 5 4
 now i want to put the first coulmn ie 1 in the positions 1 3 5 in a new matrix
                                                     3 then 2nd coulmn in the position 2
                                                     2 and 8 3rd coulmn at 6 7 ,final one at 4
 desired output
       1 2 1 4 1 3 3 2
       3 4 3 7 3 5 5 4
       2 4 2 4 2 5 5 4

Subject: working with matrix

From: anil chandy

Date: 17 Feb, 2009 05:36:01

Message: 7 of 13

there is some problem when what i typed gets displayed in the forum...

Subject: working with matrix

From: anil chandy

Date: 17 Feb, 2009 05:37:01

Message: 8 of 13

"its a mistake.. wat i actually meant was i ve a matrix A=[ 4 6 4 9 4 8 8 6 ]
and another matrix B=[4 4 4 6 6 8 8 9] which is formed by grouping the similar terms in D .. and another matrix c=[1 3 5 2 8 6 7 4] which denotes the indices of 4 6 8 and 9 in the matrix D..
i got one more matrix 1 2 3 4
                                   3 4 5 7
                                   2 4 5 4
 now i want to put the first coulmn ie 1 in the positions 1 3 5 in a new matrix
                                                     3 then 2nd coulmn in the position 2
                                                     2 and 8 3rd coulmn at 6 7 ,final one at 4
 
desired output
       1 2 1 4 1 3 3 2
       3 4 3 7 3 5 5 4
       2 4 2 4 2 5 5 4

Subject: working with matrix

From: Matt Fig

Date: 17 Feb, 2009 06:03:01

Message: 9 of 13

A=[ 4 6 4 9 4 8 8 6 ]
[B, C] = sort(A);
idx = cumsum(logical([1 diff(B)]));
T = [ 1 2 3 4;3 4 5 7;2 4 5 4];

output = T(:,accumarray(C',idx',[],@(x) x))



'nu lbb@aoh.ammpo eY avoo n khgaa'eau oyptt iMEca:eeochtil

Subject: working with matrix

From: Matt Fig

Date: 17 Feb, 2009 06:17:01

Message: 10 of 13

Another approach calls sort instead of accumarray.

[B, C] = sort(A);
idx = cumsum(logical([1 diff(B)]));
[idx2,idx2] = sort(C)
T(:,idx(idx2))




8}csck_mr}m>m%jm`mWf_csg_e,}}ikcw_nrlaCf%lj}t}afKg}m}___rn`

Subject: working with matrix

From: anil chandy

Date: 17 Feb, 2009 17:57:01

Message: 11 of 13

idx = cumsum(logical([1 diff(B)]));
i didnt undestand this line..

Subject: working with matrix

From: anil chandy

Date: 17 Feb, 2009 17:58:02

Message: 12 of 13

and why ur using the variable x?

Subject: working with matrix

From: Matt Fig

Date: 17 Feb, 2009 18:10:19

Message: 13 of 13

"anil chandy" <anilvchandy@gmail.com> wrote in message <gnetra$327
> and why ur using the variable x?

The @(x) x is an anonymous function that simply returns what it is fed. That is the way accumarray works for this type of problem.
To understand the other line, take it apart and look at it's components (a technique which will serve you well with Matlab).



idx = cumsum(logical([1 diff(B)]));

can be expanded to:

tmp1 = diff(B) % Look at the help for diff if needed.
tmp2 = [1 tmp1]
tmp3 = logical(tmp2) % Look at the help for logical if needed.
idx = cumsum(tmp3) % Look at the help for cumsum if needed.

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