# Convert 2D data to 3D matrix

10 views (last 30 days)
Behzad Navidi on 18 Feb 2020
Edited: Raymond MacNeil on 19 Feb 2020
I have a 34200 x 4 table. This table shows the 30 years monthly amount of pr (precipitation) in some latitudes and longitudes. So the columns are lat, lon, date, and pr. I want to convert it to a 3D matrix in this format: longitude x latitude x month. So the output should be longitude x latitude x 360. I wanted to use reshape but an error says I cant using reshape in the table, So I wanted to use table2array then use reshape but another error says:
Unable to concatenate the specified table variables.
Caused by:
Error using datetime/horzcat (line 1334)
All inputs must be datetimes or date/time character vectors
or date/time strings.
Also, I'm not sure how I can build a correct 3D matrix because I have many latitude and longitude that have different pr value every month. Do you have any Idea? Thanks

Raymond MacNeil on 18 Feb 2020
Do you mean you wish to add 'sheets'?
I'm unclear what you want to happen with the PR data. Where do you want this? I see what you mean by 360, however: every 360 rows is a unique set of lat and long coordinates. So, I can see why you'd want 360 sheets.
Specify the size of the matrix you want...
e.g.,
data = [x, y, z]
Then tell us what is contained within a single sheet (the x- and y-dimensions).
Behzad Navidi on 18 Feb 2020
Dear Raymond,
when Y is lon, X is lat and time is 360 pages, and numbers are pr. As it is shown non of any latitude and longitud numbers write in 3d matrix, just pr data.

KSSV on 18 Feb 2020
If T is your Table...you can access the data of the column using T.(1) , T.(2), T.(3) etc.,. If you have column names....you can access using.. T.lon, T.lat, T.date etc......
First convert your date into datevec using datenum, datevec, datetime functions. After conversion, you can filter them using the months. A striaght reshape may not work.

Behzad Navidi on 18 Feb 2020
newC=reshape(locations_lat, locations_lon, pr, dates);
% And here is error:
Dot indexing is not supported for variables of this type.
Error in datetime/reshape (line 1360)
this.data = reshape(this.data,varargin{:});
KSSV on 18 Feb 2020
YOu cannot use Reshape like that....
iwant = reshape(matrix,m,n) ;
Raymond MacNeil on 19 Feb 2020
You have not answered my question. Please give a SPECIFIC example of how you would like a single sheet to look, and how you wish to have the data organized.
Is this, perhaps, what you are going for?
30 x 12 x 95 OR 12 x 30 x 95
Where:
30 is the number of unique years in in the data set;
12 is the month of the year
95 is the number of the unique long/lat coordinate sets.
*EDIT*
Okay, if you want time to be your third dimension I can see wh that is 360 (12*30 unique time measurements per lat/long pair), but you have to explain how you want the Pr data to be organized. It cannot be long x lat because that is 95 x 95, and your data do not conform to that structure.