KatyB
KatyB

Reputation: 3980

re-formatting a matrix in matlab

This is a simplistic example of a problem I am facing:

depth = [0:1:20]';
data = rand(1,length(depth))';

d = [depth,data];
d = [d;d;d];

Consider the matrix 'd'. Here we have depth in the first column followed by temperature measurements recorded at that depth in column 2 (in this example we have 3 days of data). How could I alter this matrix so that each column represents a specific depth and each row represents time. So, finally I should have 3 rows with 21 columns.

Upvotes: 0

Views: 204

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78306

If I understand correctly your array d has the data for day 1 in rows 1:21, for day 2 in rows 22:42, and so on. Column 1 of d holds the depths (3 times), and column 2 holds the measurements.

One way to get the results in the form you want is to execute:

d2 = reshape(d(:,2),21,3)'; % note the ' for transposition here

This leaves you with an array with 3 rows and 21 columns. Each column represents the measurements for one depth, each row the measurements for one day.

Upvotes: 1

Related Questions