How to squeeze specific value from 4D array?
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Adam Danz
2024-5-16
编辑:Adam Danz
2024-5-16
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
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)
lons = lon(col)
depths = depth(dim3Idx)
years = year(dim4idx)
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 个评论
Adam Danz
2024-5-17
>... 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;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!