Got Questions? Get Answers.
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:
beginner's question (vectors and matrices)

Subject: beginner's question (vectors and matrices)

From: rose lans

Date: 5 Dec, 2008 05:16:45

Message: 1 of 4

Hi everybody,
I've been working on the following problem regarding vectors:

a=[0 0 0 1 1 1 2 2 2];
b=[1 2 3 1 2 3 1 2 3];
c=[1 2 3 4 5 6 7 8 9];
I need to construct a matrix r such that r(i,j) is the value of c that corresponds
to i-th distinct value of a and j-th distinct value of b.
The distinct values of a are [0 1 2],
The distinct values of b are [1 2 3],
The resulting matrix is
r=
[1 2 3
 4 5 6
 7 8 9].

here is my code:
function [I1,I2,r]=R2(a,b,c)
u1=unique(a);
u2=unique(b);
 for i=1:length(u1)
    for j=1:length(u2)
     % u1(i) ith distinct value of a
     % u2(j) jth distinct value of b
 I1=find(a==u1(i));
 I2=find(b==u2(j));
 r=[c(I1);c(I2)];
    end
  
 end
for my output i get
I1 =

     7 8 9


I2 =

     3 6 9


r =

     7 8 9
     3 6 9
I can't figure out what the problem is. I would appreciate any help.
Thank you,
Rose.

Subject: beginner's question (vectors and matrices)

From: swgillan

Date: 5 Dec, 2008 06:23:26

Message: 2 of 4

On Dec 4, 9:16=A0pm, "rose lans" <byruse...@yahoo.com> wrote:
> Hi everybody,
> I've been working on the following problem regarding vectors:
>
> a=3D[0 0 0 1 1 1 2 2 2];
> b=3D[1 2 3 1 2 3 1 2 3];
> c=3D[1 2 3 4 5 6 7 8 9];
> I need to construct a matrix r such that r(i,j) is the value of c that co=
rresponds
> to i-th distinct value of a and j-th distinct value of b.
> The distinct values of a are [0 1 2],
> The distinct values of b are [1 2 3],
> The resulting matrix is
> r=3D
> [1 2 3
> =A04 5 6
> =A07 8 9].
>
> here is my code:
> function [I1,I2,r]=3DR2(a,b,c)
> u1=3Dunique(a);
> u2=3Dunique(b);
> =A0for i=3D1:length(u1)
> =A0 =A0 for j=3D1:length(u2)
> =A0 =A0 =A0% =A0 u1(i) ith distinct value of a
> =A0 =A0 =A0% =A0 u2(j) jth distinct value of b
> =A0I1=3Dfind(a=3D=3Du1(i));
> =A0I2=3Dfind(b=3D=3Du2(j));
> =A0r=3D[c(I1);c(I2)];
> =A0 =A0 end
>
> =A0end
> for my output i get
> I1 =3D
>
> =A0 =A0 =A07 =A0 =A0 8 =A0 =A0 9
>
> I2 =3D
>
> =A0 =A0 =A03 =A0 =A0 6 =A0 =A0 9
>
> r =3D
>
> =A0 =A0 =A07 =A0 =A0 8 =A0 =A0 9
> =A0 =A0 =A03 =A0 =A0 6 =A0 =A0 9
> I can't figure out what the problem is. =A0I would appreciate any help.
> Thank you,
> Rose.

Can you just use "reshape" command on c?

as in:

m =3D length(unique(a));
n =3D length(unique(b));

r =3D reshape(c,m,n)';

that will result in:

r =3D

     1 2 3
     4 5 6
     7 8 9

Subject: beginner's question (vectors and matrices)

From: Miroslav Balda

Date: 5 Dec, 2008 08:06:04

Message: 3 of 4

"rose lans" <byrusel42@yahoo.com> wrote in message <ghadfs$q39$1@fred.mathworks.com>...

Hi Rose,

It follows from your result yhat you want yo get (3x3) matrix r without zeros. Is it true?
Hence, there is the most efficient code (after declaring a, b, c):

d = unique([a b c]) % The first element is zero
r = zeros(3)
r(:) = d(2:end)
r = r'

Mira

Subject: beginner's question (vectors and matrices)

From: rose lans

Date: 7 Dec, 2008 01:54:02

Message: 4 of 4

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <ghandc$9tu$1@fred.mathworks.com>...
> "rose lans" <byrusel42@yahoo.com> wrote in message <ghadfs$q39$1@fred.mathworks.com>...
>
> Hi Rose,
>
> It follows from your result yhat you want yo get (3x3) matrix r without zeros. Is it true?
> Hence, there is the most efficient code (after declaring a, b, c):
>
> d = unique([a b c]) % The first element is zero
> r = zeros(3)
> r(:) = d(2:end)
> r = r'
>
> Mira
>
>
>
Thank you, but this works only for this particular c, if I change vectors to something different,the above code gives error
??? In an assignment A(:) = B, the number of elements in A and B
must be the same.

Error in ==> R4 at 4
r(:) = d(2:end);

I am so confused....

Tags for this Thread

No tags are associated with 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