Creating a 3D-array out of a 2D-array

1 回表示 (過去 30 日間)
Arne
Arne 2014 年 11 月 24 日
回答済み: Raghava S N 2025 年 1 月 9 日
I have a large 2D-array (31100 x 4) built up like this:
Intensity | X | Y | Z
out of this array I would like to create a 3D-array to visualize it as a 3D-image.
I would somehow have to transform the 2D into a 3D-array. Since there will be plenty of points where there is no intensity value available all of these points should be zero values.
Side information:
min(x) = -152.0120; max(x) = 161.4350; min(y) = -256.2560; max (y) = -52.3801; min(z) = -428.5920; max (z) = -152.4210;
Because of the larger number of decimal places I might have to round up. Yet, this would possibly lead to several intensities being assigned to the same coordinate combination. Logically, I would have to add up the intensities in these cases.
This makes everything really complicated.
I would be happy if someone could teach me how to transform a 2D into a 3D array with zero-values for spots without intensity values.
Thanks for reading.

回答 (1 件)

Raghava S N
Raghava S N 2025 年 1 月 9 日
Hi,
To create the 3D array that displays the accumulated intensity at particular co-ordinates, you can create an empty 3D array (filled with zeros), and then loop through the "Intensity" column and add each intensity to its respective co-ordinate. This way, if there are duplicate co-ordinates generated due to rounding, the intensities add up, and empty co-ordinates will have zero intensity.
Here is some sample code to get you started-
% Sample data (replace this with your actual data)
data = rand(31100, 4); % Replace with your actual 2D array
Extract the data -
% Extract columns
intensity = data(:, 1);
X = data(:, 2);
Y = data(:, 3);
Z = data(:, 4);
Define the grid ranges and size -
% Define grid ranges
x_min = floor(min(X));
x_max = ceil(max(X));
y_min = floor(min(Y));
y_max = ceil(max(Y));
z_min = floor(min(Z));
z_max = ceil(max(Z));
% Define the size of the 3D array
x_size = round((x_max - x_min)) + 1;
y_size = round((y_max - y_min)) + 1;
z_size = round((z_max - z_min)) + 1;
% Initialize the 3D array
volume = zeros(x_size, y_size, z_size);
Map the Intensities -
% Map intensities to the 3D grid
for i = 1:length(intensity)
% Calculate grid indices
x_idx = round((X(i) - x_min)) + 1;
y_idx = round((Y(i) - y_min)) + 1;
z_idx = round((Z(i) - z_min)) + 1;
% Accumulate intensity values
volume(x_idx, y_idx, z_idx) = volume(x_idx, y_idx, z_idx) + intensity(i);
end
Hope this helps!

カテゴリ

Help Center および File ExchangeGeographic Plots についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by