Sorting a 64 x6 table

1 view (last 30 days)
Yannick Tabot Njami
Yannick Tabot Njami on 13 Mar 2019
Dear All i need help with sorting a table and also new to matlab your time is appreciate
Please consider the attachment
Here is my algorithm :
ycoordinate= [20 50 80 110 140 170 200 230]
  • find all index example when y= 20
  • get the x coordinate values corresponding to to the indices y=20
  • get the corresponding mean_velocity,std_velocity, mean_pd ,std_pd vlalues
  • get them as a pair. meaning that for case when y=20 ,
  • x = [20 110 140 170 200 20 230 50 80] and mean_velocity=[1.3696 57.5733 57.7197 57.7133 58.0707 58.0194 58.1603 57.9150 57.7572]
  • then sorting them in ascending or descending order as
  • x=[20 20 50 80 110 140 170 200 230] mean_velocity=[1.3696 58.0194 57.9150 57.7572 57.5733 57.7197 57.7133 58.0707 58.1603 ]
  • and then plot mean_velocity against x. i want to do this for the the std_velocity, mean_pd ,std_pd as well.
  • i thought about a for loop but dont know how to go about it . please any help will be greatly appreciated
  • many thanks in advance
  3 Comments
Guillaume
Guillaume on 13 Mar 2019
For table, sortrows is probably more appropriate than sort. You can pass the variable names directly to sortrows to prioritise the sort columns:
sortrows(sometable, {'ycoordinates', 'xcoordinates'}) %to sort first by y, then x.
Guillaume
Guillaume on 13 Mar 2019
Yannick comment posted as an answer moved here (it's just code with no explanation):
Average_Table = table(xcoordinate,ycoordinate,mean_velocity,std_velocity, mean_pd ,std_pd);
for jj = 20 :30 :230
yindex=find(ycoordinate == jj);
%Deleting the first index to eliminat the offset value at y=20
if jj ==20
yindex(yindex==1)=[];
end
% getting just a table dor x and mean_velocity
newX=(xcoordinate(yindex));
newVY =mean_velocity(yindex);
%generated table as a pair
tableXY =[newX newVY]
% soting out the table
preallocating memory for sorted table
sortedtable =zeros(length(tableXY),2)
sortedtable(:,1)=sort(tableXY(:,1))
sortedX = sortedtable (:,1)
%Going through each table to look for corresponding indeces and match .
for N =1:length(tableXY)
sortedtable(N,2:end) =tableXY(find(ismember(tableXY(:,1),sortedtable(N))),2)
% sorted Y coordinates
sortedY = sortedtable(:,2);
end
hold on
plot(sortedX,sortedY,'color',rand(1 ,3),'marker','*');
xlabel('x position');
ylabel('mean velocity');
legend(['mean velocity against position =' num2str(jj)]);
drawnow;

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 13 Mar 2019
If I understood correctly what you want, you basically want a plot for each unique y value which are the other variables against x. The easiest way to achieve this is:
  • create a function to plot the variables against x, ignoring y for now. That function therefore takes as input x, and the other variables. E.g (tailor as required):
function myplot(x, varargin) %saved in its own m file
%inputs:
% x: a possibly unsorted array of x coordinates. Will be sorted before plotting
% 4 more inputs (for now): mean velocity, std velocity, mean pd, std pd.
%all inputs are column vector as they come from a table
assert(numel(varargin) == 4, 'Only 4 variables expected');
[x, order] = sort(x);
plotvars = [varargin{:}]; %concatenate the other 4 vectors into a 4 column matrix
plotvars = plotvars(order, :); %and sort in the same order as x
plot(x, plotvars);
legend({'mean velocity', 'std velocity', 'mean pd', 'std pd'});
end
  • use rowfun to call the above function for each group of unique y:
rowfun(@myplot, thetable, 'GroupingVariables', 'ycoordinates', 'InputVariables', {'xcoordinates', 'mean_velocity', 'std_velocity', 'mean_pd', 'std_pd'}); %group by y. For each group, pass x and the other columns to the function
Done!
  11 Comments
Yannick Tabot Njami
Yannick Tabot Njami on 15 Mar 2019
Here is what i get :untitled.png
Yannick Tabot Njami
Yannick Tabot Njami on 15 Mar 2019
here is what i expect :dataplot.png

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!