Thread Subject:
Convert 3 vectors to a matrix

Subject: Convert 3 vectors to a matrix

From: Tom

Date: 1 Apr, 2011 06:49:04

Message: 1 of 7

Hello all!

I wan't to convert 3 vectors (a, b, c) to one matrix (A). Assume a respresent the columns of A, b the rows of A and c are the values inside A.

Small example:

a = [1 1 3 4]
b = [1 4 2 3]
c = [1.2 5.2 10.2 6.7]

So the matrix A should be something like this:

A = [1.2 0 0 0;
         0 0 10.2 0;
         0 0 0 6.7;
       5.2 0 0 0]

Another difficulty is that the values in a and b are not just integers, but they could be 1.4, 5.3, ...

Is there a standard MATLAB commando for this?

Thx

Subject: Convert 3 vectors to a matrix

From: Herve

Date: 1 Apr, 2011 09:09:04

Message: 2 of 7

n_column = max(a);
n_array = max(b);
A = zeros(n_array,n_column);
for i=1:length(a)
    A(b(i),a(i)) = c(i);
end

Subject: Convert 3 vectors to a matrix

From: Matt J

Date: 1 Apr, 2011 09:28:04

Message: 3 of 7


dims=[max(b), max(a)];
A=zeros(dims);
A(sub2ind(dims,b,a))=c,

Subject: Convert 3 vectors to a matrix

From: Jos (10584)

Date: 1 Apr, 2011 09:36:04

Message: 4 of 7

"Tom" wrote in message <in3sh0$s0c$1@fred.mathworks.com>...
> Hello all!
>
> I wan't to convert 3 vectors (a, b, c) to one matrix (A). Assume a respresent the columns of A, b the rows of A and c are the values inside A.
>
> Small example:
>
> a = [1 1 3 4]
> b = [1 4 2 3]
> c = [1.2 5.2 10.2 6.7]
>
> So the matrix A should be something like this:
>
> A = [1.2 0 0 0;
> 0 0 10.2 0;
> 0 0 0 6.7;
> 5.2 0 0 0]
>
> Another difficulty is that the values in a and b are not just integers, but they could be 1.4, 5.3, ...
>
> Is there a standard MATLAB commando for this?
>
> Thx


A = full(sparse(b,a,c))
A = accumarray([b(:) a(:)],c)

~ Jos

Subject: Convert 3 vectors to a matrix

From: Matt J

Date: 1 Apr, 2011 09:49:04

Message: 5 of 7

"Jos (10584)" wrote in message <in46a4$5ms$1@fred.mathworks.com>...
>
>
> A = full(sparse(b,a,c))
> A = accumarray([b(:) a(:)],c)


Just to clarify, I guess, each line above is a separate solution. It's not a 2-line piece of code.

Subject: Convert 3 vectors to a matrix

From: Tom

Date: 1 Apr, 2011 10:25:05

Message: 6 of 7

Thx for all the help, I'll will try all the suggestions!

Subject: Convert 3 vectors to a matrix

From: Jos (10584)

Date: 1 Apr, 2011 21:09:07

Message: 7 of 7

"Matt J" wrote in message <in472g$h1g$1@fred.mathworks.com>...
> "Jos (10584)" wrote in message <in46a4$5ms$1@fred.mathworks.com>...
> >
> >
> > A = full(sparse(b,a,c))
> > A = accumarray([b(:) a(:)],c)
>
>
> Just to clarify, I guess, each line above is a separate solution. It's not a 2-line piece of code.

True! Thanks Matt J for noting this.

~ Jos

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vector matrix Tom 1 Apr, 2011 02:54:08
rssFeed for this Thread

Contact us