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:
Converting z data to matrix

Subject: Converting z data to matrix

From: Ian

Date: 26 Jan, 2013 15:32:07

Message: 1 of 4

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

This is just an example data set my data set is much bigger than this but I figured if there is a way of converting z into the example matrix and could apply the same method to my much bigger data set.

Any help would be much appreciated.

Subject: Converting z data to matrix

From: dpb

Date: 26 Jan, 2013 15:51:24

Message: 2 of 4

On 1/26/2013 9:32 AM, Ian wrote:
...
> ... 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
...

 >> z= [8 4 9 7 3];
 >> Z=fliplr(diag(z,0))
Z =
      0 0 0 0 8
      0 0 0 4 0
      0 0 9 0 0
      0 7 0 0 0
      3 0 0 0 0
 >>

--

Subject: Converting z data to matrix

From: Ian

Date: 26 Jan, 2013 16:19:08

Message: 3 of 4

"Ian" wrote in message <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
>
> This is just an example data set my data set is much bigger than this but I figured if there is a way of converting z into the example matrix and could apply the same method to my much bigger data set.
>
> Any help would be much appreciated.

Subject: Converting z data to matrix

From: Steven_Lord

Date: 28 Jan, 2013 16:11:03

Message: 4 of 4



"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
receive the z you requested.

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
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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