Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Converting z data to matrix
Replies: 3   Last Post: Jan 28, 2013 11:11 AM

 Messages: [ Previous | Next ]
 Steven Lord Posts: 18,038 Registered: 12/7/04
Re: Converting z data to matrix
Posted: Jan 28, 2013 11:11 AM

"Ian " <ikirby23@yahoo.com> wrote in message
news:ke0stn\$is9\$1@newscl01ah.mathworks.com...
> Hi guys,
>
> I wonder if any one could be so kind to help me with what I'm sure is
> probably relatively straight forward. If I have x,y, and z data as
> follows:
>
> X= 1 2 3 4 5
> y= 1 2 3 4 5
> z= 8 4 9 7 3
>
> Is there any way of making z into the matrix from the above:
>
> Z= 0 0 0 0 3
> 0 0 0 7 0
> 0 0 9 0 0
> 0 4 0 0 0 8 0 0 0 0

How do you get from x, y, and z to this matrix? If x(k) and y(k) are the
indices of the element whose value is z(k) then this should result in a
diagonal matrix NOT an off-diagonal. If instead you flipped x then we'd

x= [5 4 3 2 1].';
y= [1 2 3 4 5].';
z= [8 4 9 7 3].';
accumarray([x, y], z)

Alternately, if your matrix is going to be sparsely populated (only a small
percentage of the elements will be nonzero) then look at the help for SPARSE
and use it instead of ACCUMARRAY or use ACCUMARRAY with the ISSPARSE input
set to true.

--
Steve Lord
slord@mathworks.com