How to squeeze specific value from 4D array?

4 vues (au cours des 30 derniers jours)
Gurumoorthi K
Gurumoorthi K le 16 Mai 2024
Commenté : Adam Danz le 17 Mai 2024
I have an 4D (lon, lat, depth level, years) array output of temperature_data. Now I want to squeeze the value only "25 (truely constant)" from the 4D array. Note that, here i want to extract the value "25" from each grid, each depth level, each years.
Thanks in advance

Réponse acceptée

Adam Danz
Adam Danz le 16 Mai 2024
Modifié(e) : Adam Danz le 16 Mai 2024
It seems like you are asking for a way to identify and retrieve every instance where the temperature value is exactly 25 within the 4D array and to return the lon, lat, depth, and years values that correspond to those locations within the array. If my interpretation of the question is correct, you're not looking to reshape or reduce the dimensions of the array (as squeeze might imply).
0. Create 4D demo array that includes values of 25 and define the 4 dimensions
rng default
A = randi(25,5,4,6,3);
lat = [0 45 90 -45 -90]; % dim 1
lon = [50 100 150 180]; % dim 2
depth = [10 20 30 40 50 60]; % dim 3
year = [1900 1950 2000]; % dim 4
1. Find the linear index of each value of 25.
criticalValue = 25;
linIdx = find(A==criticalValue);
2. Convert the linear index to subscript indices (row, column, 3rd-dim and 4th-dim indices)_
[row,col,dim3Idx,dim4idx] = ind2sub(size(A),linIdx);
3. Return the lat, lon, depth, and year for each value of 25 in the array. This assumes the 1st dimension is latitude, 2nd is longitude, 3rd is depth, and 4th is year.
lats = lat(row)
lats = 1x12
-90 45 90 45 0 0 -90 45 90 -45 -90 -45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lons = lon(col)
lons = 1x12
100 150 180 50 100 150 180 180 150 150 100 180
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
depths = depth(dim3Idx)
depths = 1x12
10 10 60 10 60 10 10 30 40 50 60 60
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
years = year(dim4idx)
years = 1x12
1900 1900 1900 1950 1950 2000 2000 2000 2000 2000 2000 2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If the goal is to remove values of 25 from the 4D array, there are a couple of options. Assuming the values are spead throughout the array, you can't remove the data without changing the shape and size of the array and that will make it difficult to understand the lat, lon, depth, and year definitions for each dimension. Instead, you could either replace those values with missing a value indicator (NaN) or you can use an interpolation techique to replace those value. To replace with NaNs, do step 1 above to compute linIdx and then A(linIdx)=NaN;.
  2 commentaires
Gurumoorthi K
Gurumoorthi K le 17 Mai 2024
Dear Adam Danz , Thanks for your reply.
Here, I would like to clarify you that, I have temperature data with lat, lon, depth, time dimension. Now I want extract the value only temperature value '25' wherewhere presents (search each grid, each depth level, each year. finally get 4D. Make it NaN if it is below or above 25.
Size of my data: 360 76 42 41
Adam Danz
Adam Danz le 17 Mai 2024
>... Make it NaN if it is below or above 25.
In that case, assuming your array variable is A,
criticalValue = 25;
A(A~=criticalValue) = NaN;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Tags

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by