简体   繁体   中英

octave: how to arrange measurement values in format “x y value\n” into a matrix?

I have data from a measurement. It is stored in the format xy value

  x         y      value

   64        4     2743
   64        8     3531
   64       16     4543
   64       32     5222
   64       64     5730
  128        4     2778
  128        8     3500
  128       16     4657
  etc

How can I rearrange this into a matrix of format

    y= 4    8    16   32   64
x=
64    2743 3531 4543 5222 5730
128   2778 3500 4657 …    …  
256   …    …    …    …    …
512   …    …    …    …    …

in octave?

Here I am assuming you have a measurement for every combination of values of x and y, else we cannot create a matrix. Also, the sequence of x and y values should repeat just like your example.

% raw data is in a matrix D
% Unique will tell us all the values in the order in which they appear
x = unique(D(:, 1));
y = unique(D(:, 2));
% Reshape can create a matrix from a vector in column-major order
d = reshape(D(:, 3), length(y), length(x))'; % note transpose

newData = [x d];

Output:

octave> D
D =

     64      4   2743
     64      8   3531
     64     16   4543
     64     32   5222
     64     64   5730
    128      4   2778
    128      8   3500
    128     16   4657
    128     32   4512
    128     64   7854
octave> y'
ans =

    4    8   16   32   64

octave> newData
newData =

     64   2743   3531   4543   5222   5730
    128   2778   3500   4657   4512   7854

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM