How do I sort a 3D matrix where the third dimension is a function of the other two dimensions?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a data matrix 'DATA' which is of shape mxnx3. This matrix is the result of a sensor sensitivity calculation.
The calculation is done in spherical coordinates and here's some pseudocode to work through the issue:
theta = 0:.1:89.9;
phi = 0:.25:360;
theta(1) = 0.00001;
phi(1) = 0.00001;
[PHI,THT] = meshgrid(phi,theta); % Mesh grid of the test angles
range = 1500; % Range for spherical coordinate pairs
% A bunch of calculations...
% ...
% ...
d_out = 10*rand(size(PHI)); % A fake output matrix of the calculation, matches size of mesh grid.
The calculation is done in spherical coordinates because it allows for some simplifications which greatly simplifies the math and reduces the number of calculations.
The function then converts the meshgrid of angles to along and across track angles as this is a more typical way to consider this sensor.
%% Calculate field points used for calculation
x = range .* sind(THT) .* cosd(PHI);
y = range .* sind(THT) .* sind(PHI);
z = range .* cosd(THT);
%% Convert to Along and Across Track
% Note that right hand rule applies. Projected angles are measured off
% of the array normal (z axis). A sample in the second or third
% quadrant will have a negative y value making the atan calculation
% negative. However, angles towards the negative y axis are considered
% positive according to the right hand rule (do the hand thing, I
% promise).
alng = atan2d(x,z);
acrx = atan2d(-y,z);
DATA(:,:,1) = alng;
DATA(:,:,2) = acrx;
DATA(:,:,3) = d_out;
% Here is the point where the function returns the data
Now when I visualize this, I typically use pcolor, and the results is nicely presented in along vs across track
figure;
pcolor(DATA(:,:,1),DATA(:,:,2),DATA(:,:,3))
shading flat;
colorbar;
xlabel('Across angle')
ylabel('Along angle')
However, apart from visualizing the data, I want to copy it to a table where the format is in along vs across angle. exactly like the figure.
This I think requires multiple sort calls. This is what I tried and is obviously is not working:
alng = DATA(:,:,1); % Breaking out the data for continuity
acrx = DATA(:,:,2);
d_out = DATA(:,:,3);
figure
imagesc(alng)
colorbar;
set(gca,'YDir','normal')
title('Along angles')
figure
imagesc(acrx)
colorbar;
set(gca,'YDir','normal')
title('Across angles')
I want the results sorted by angle, sort like:
[tmp1,inx] = sort(alng,1);
figure;
imagesc(tmp1)
colorbar;
set(gca,'YDir','normal')
title('Sorted along angles')
[tmp2,inx] = sort(acrx,2);
figure;
imagesc(tmp2)
colorbar;
set(gca,'YDir','normal')
title('Sorted across angles')
Below was what I though was what would accomplish what I want:
% Sort via along angles
[tmp1,inx] = sort(alng,1);
tmp2 = acrx(inx);
tmp3 = d_out(inx);
% Sort via across angles
[tmp2,inx] = sort(tmp2,2);
tmp1 = tmp1(inx);
tmp3 = tmp3(inx);
% Sort via along angles finally
[tmp1,inx] = sort(tmp1,1);
tmp2 = tmp2(inx);
tmp3 = tmp3(inx);
Any advice would be greatly appreciated since I am definitely missing something
0 Kommentare
Antworten (1)
Gowtham
am 29 Dez. 2023
Hello Mike,
I understand that you are trying to sort the available data along and across the angles as required.
When sorting a matrix in MATLAB, the function returns both the sorted array and the sorting indices. However, these indices are dimension specific. To sort other matrices like acrx and d_out in the same way, you must apply the indices correctly, considering the dimensionality of your data. This approach works straightforwardly for 1D arrays but requires careful application for 2D matrices or higher dimensions.
For a sample demonstration of the above process, kindly refer to the following code snippet:
[m, n, ~] = size(DATA);
sorted_DATA = DATA;
% Sort by along angles (first dimension)
for j = 1:n
[sorted_alng, inx_alng] = sort(DATA(:, j, 1));
for k = 1:3
sorted_DATA(:, j, k) = DATA(inx_alng, j, k);
end
end
% Create a 2D matrix that combines the sorting keys (alng and acrx) and sort it.
combined_keys = reshape(sorted_DATA(:,:,1:2), [], 2);
[~, inx_combined] = sortrows(combined_keys, [1, 2]);
% Apply the combined sorting index to the entire DATA matrix.
sorted_DATA_combined = reshape(sorted_DATA, [], 3);
sorted_DATA_combined = sorted_DATA_combined(inx_combined, :);
% Reshape back to the original 3D matrix form.
sorted_DATA = reshape(sorted_DATA_combined, m, n, 3);
% Now, sorted_DATA(:,:,1) is sorted by alng, then acrx, and the same order is applied to d_out.
figure;
imagesc(sorted_DATA(:, :, 1))
colorbar;
set(gca,'YDir','normal')
title('Sorted along angles')
Below is the output of the data sorted along angles:
Kindly refer to the following MathWorks Documentation(s) for further information on how to use ‘sort’ https://www.mathworks.com/help/matlab/ref/sort.html
Hope this helps.
Best Regards,
Gowtham
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!