sorting a matrix then applying the sort to another matrix

2 views (last 30 days)
Hello, i am working in spherical coordinates and want to sort my data to look along a cross section of my data to plot radius vs. theta for a fixed azimuth.
I have three 2D matrices (mxn) A = elevation, B = azimuth, C = radius. There are repeated cases of [A,B] with a unique C. The data in A and B is rounded to "bin" the results. I have then converted to cartesian coordinates to view [x,y,z]
How do I create and sort the new X,Y,Z matrix such that it is ordered like the plot, i.e. X(1,1), Y(1,1) ,Z(1,1) is the top corner pixel in my image.
  4 Comments
Guillaume
Guillaume on 25 Jan 2019
sortrows is not going to help with calculating the sum of the radii for a given elevation, azimuth combination.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 24 Jan 2019
Edited: Guillaume on 24 Jan 2019
"How do I use this data to create a new 3 column matrix where column 1 is elevation column 2 is azimuth, and each row is a unique elevation, azimuth combination. Then the third column is the sum of all radius values at that specific elevation and azimuth"
Like Stephen, I do not understand your original question but the bit above is easy;
%inputs:
%elevation: Your A matrix with a more useful name. Can be any shape and size
%azimuth: Your B matrix with a more useful name. Same size as elevation
%radius: Your C matrix. Same size as the others
[urows, ~, subs] = unique([elevation(:), azimuth(:)], 'rows');
result = [urows, accumarray(subs, radius(:))]; %default function for accumarray is sum which is exactly what you want
All done!
Note that for clarity I would store the result in a table:
result = array2table(result, 'VariableNames', {'elevation', 'azimuth', 'sumradii'})

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!