简体   繁体   English

MATLAB:2D 数据到 3D ! 如何?

[英]MATLAB: 2D DATA TO 3D ! How to?

I have a 34200 x 4 table.我有一个34200 x 4桌子。 This table shows the 30 years monthly amount of pr (precipitation) in some latitudes and longitudes.该表显示了某些纬度和经度的 30 年月 pr(降水量)量。 So the columns are lat , lon , date , and pr .所以列是latlondatepr I want to convert it to a 3D matrix that longitude x latitude x month .我想将其转换为longitude x latitude x month的 3D 矩阵。 I attach my table.我附上我的桌子。 Please tell me how to do it I'm a beginner.请告诉我怎么做我是初学者。 If I don't want to convert it based on the month I could but this issue is so complicated for me.如果我不想根据月份转换它,我可以但这个问题对我来说太复杂了。 Please look at my table it's only 235 KB I upload it to my DropBox so please click on Open in the top right side and click download.请看我的表格,它只有 235 KB我将它上传到我的 DropBox,所以请点击右上角的打开并点击下载。

Here is my image这是我的图片

Inspecting your data, your latitude and logitude values actually represent 95 unique locations which are scattered seemingly randomly.检查您的数据,您的纬度和经度值实际上代表了 95 个看似随机分散的独特位置。 You can see that in the figure below.您可以在下图中看到这一点。

length(unique(C.lat)) % 95
length(unique(C.lon)) % 95
scatter(C.lat, C.lon)

在此处输入图片说明

If the locations were spaced in a grid, it would make sense to use lat and lon as the axes of a data matrix.如果位置在网格中间隔开,则使用latlon作为数据矩阵的轴是有意义的。 But instead, it is better to use only one axis representing the unique locations.但是,最好仅使用一个轴来表示唯一位置。 This then leaves you with a second axis representing the date.然后,这为您留下了代表日期的第二个轴。

length(unique(C.date)) % 360
360 * 95 % 34200 - the number of values we have

Reformatting the data重新格式化数据

Therefore, I would store the data in a 2D matrix as follows.因此,我将数据存储在二维矩阵中,如下所示。

locations_lat = unique(C.lat, 'stable');
locations_lon = unique(C.lon, 'stable');
dates = unique(C.date, 'stable');
data = reshape(C.pr, length(dates), length(locations_lat)); % size 360 x 95

Then, to check that this has worked, choose a random example.然后,要检查这是否有效,请选择一个随机示例。

location_num = 27;
date_num = 242;
lat = locations_lat(location_num) % 14.68055556
lon = locations_lon(location_num) % 65.23111111
date = dates(date_num) % 2/1/2009
precipitation = data(date_num, location_num) % 16.7179

Searching for that position and date in the original tale, we have:在原始故事中搜索那个位置和日期,我们有:

9602|   14.6805555600000    65.2311111100000    '2/1/2009'  16.7179000000000

If A is your data with the 4 columns [34200x4] then you can create a 3-dimensional matrix like this:如果A是具有 4 列[34200x4]数据,那么您可以创建一个 3 维矩阵,如下所示:

B = zeros(len(A),3)
B(:, 1) = A(:, 1)
B(:, 2) = A(:, 2)
B(:, 3) = A(:, 3)

Possibly even:甚至可能:

B(:,1:3) = A(:, 1:3)

Depending how your data is set, you may need to transpose which is:根据您的数据设置方式,您可能需要转置:

B = 'B

You can map data over as long as the dimensions match.只要维度匹配,您就可以映射数据。

You can implement a for loop if you have even more columns or for dynamic data entries.如果您有更多列或用于动态数据条目,则可以实现 for 循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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