How do I compare the order of rows in cell array with a matrix?
4 views (last 30 days)
Show older comments
lil brain on 10 Dec 2022
Commented: the cyclist on 11 Dec 2022
I have a numerical array called split_points_rounded and a cell array called valid_high_IDs. The first column of split_points_rounded and the first row of valid_high_IDs should theoretically be in the same order (these are participant IDs from a study).
I am looking for a way to
1) check if they are in the same order and
2) re-order the rows in split_points_rounded according to the order in valid_high_IDs and save that in a new cell array if the two are not already in the same order.
Help is much appreciated!
You can use the ismember function to both test that the values of one array are present in the other one, and also to get their locations in that array. You can then use the output to see if they are in the sorting order you want.
Your uploaded data are not a great example, since they are already correctly sorted, but I believe this does what you want.
% % Load local file
% Load from web
% Convert the cell array with character arrays into a numeric array
idOrder = cellfun(@str2double,valid_high_IDs);
% Find the locations of one array in the other
[~,loc] = ismember(idOrder,split_points_roundedCopy(:,1));
% Sort accordingly
split_points_rounded_reordered = split_points_roundedCopy(loc,:)
Find more on Matrices and Arrays 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!