MATLAB Answers

0

Sorting a 64 x6 table

Asked by Yannick Tabot Njami on 13 Mar 2019
Latest activity Commented on by Yannick Tabot Njami on 15 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

dpb
on 13 Mar 2019
Much easier to debug and show where make either syntax or logic errors if you will actually insert the code itself, not just the description. It's mandatory to have the problem description of what you're trying to do, of course, but "show your work!", too...
As to the Q? SORT() will do what you want...you can either sort the array with _'rows'_ option or you can sort the one vector and return the sort order index to rearrange the other variables accordingly. Syntax and examples at
doc sort
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
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.

Products


Release

R2018b

1 Answer

Guillaume
Answer by 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

Guillaume
on 15 Mar 2019
It probably would make sense to create a new figure for each y, and use the y to name the graph. So I'd modify the myplot function to:
function myplot(y, x, varargin) %saved in its own m file
%inputs:
% y: the y coordinate of the plot. Due to the way rowfun works this will be a column vector of identical y
% 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');
figure;
[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'});
title(sprintf('At y = %g', y(1)));
end
and the rowfun:
rowfun(@myplot, Average_Table, 'GroupingVariables', 'ycoordinate', 'InputVariables', {'ycoordinate', 'xcoordinate', 'mean_velocity', 'std_velocity', 'mean_pd', 'std_pd'}, 'NumOutputs', 0);
"but the plot produce was very strange and not what i expected"
Well without any detail of what you expected and what you got, it's difficult to give any advice.
Here is what i get :untitled.png
here is what i expect :dataplot.png

Sign in to comment.