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 ith distinct value of a and jth 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
