Rearranging columns in a data

2 views (last 30 days)
Andrew Lapointe
Andrew Lapointe on 10 Nov 2014
Commented: the cyclist on 10 Nov 2014
Good day everyone,
I am currently trying to figure out a method to selectively re-arrange a matrix which is quite large, in order to simplify my issue I've created an example matrix using the following code
if true
% code
Matrix1=rand(21,27)
%Creates Matrix2 which is Matrix1 with labels
Matrix2=mat2dataset(Matrix1);
Matrix2(:,:);
Matrix2.Properties.Description = 'Creating Matrix1 with labels';
Matrix2.Properties.VarNames = {'A1a','A2a','A3a','A1b','A2b','A3b','A1c','A2c','A3c','B1a','B2a','B3a','B1b','B2b','B3b','B1c','B2c','B3c','C1a','C2a','C3a','C1b','C2b','C3b','C1c','C2c','C3c'}
end
(Please ignore the fact there is only one row in the image. I am aware the code gives 21 rows and not 1)
What is the best way to accomplish this? I have looked into other methods on the web but they don't seem to function. Is there a way to create a matrix based of the variable names given to Matrix2? Such that
Matrix3 = Matrix2(A1a,B1a,C1a...etc)
Any advice or help would be great. My actual matrix is much larger but this is the best way I could showcase it.

Answers (1)

the cyclist
the cyclist on 10 Nov 2014
Edited: the cyclist on 10 Nov 2014
I'm not entirely sure this is general enough for what you need, but this gives the proper sorting for your example:
A_flip = cellfun(@fliplr,A,'UniformOutput',false)
[~,sortingIndex] = sort(A_flip)
sortingIndex give the reordering you need.
  2 Comments
Andrew Lapointe
Andrew Lapointe on 10 Nov 2014
Edited: Andrew Lapointe on 10 Nov 2014
Hi there the cyclist,
I tried your method in my code and it doesn't work, I get "Undefined function or variable 'A'."
If I implement the code you gave me to this
if true
% code
Matrix1=rand(21,27)
%Creates Matrix2 which is Matrix1 with labels
Matrix2=mat2dataset(Matrix1);
Matrix2(:,:);
Matrix2.Properties.Description = 'Creating Matrix1 with labels';
Matrix2.Properties.VarNames = {'A1a','A2a','A3a','A1b','A2b','A3b','A1c','A2c','A3c','B1a','B2a','B3a','B1b','B2b','B3b','B1c','B2c','B3c','C1a','C2a','C3a','C1b','C2b','C3b','C1c','C2c','C3c'}
%The cyclist response try
A_flip = cellfun(@fliplr,Matrix1,'UniformOutput',false)
[~,sortingIndex] = sort(A_flip)
end
I get the following error "Error using cellfun Input #2 expected to be a cell array, was double instead."
Can you please elaborate on how you got this to work?
Thanks for your response
the cyclist
the cyclist on 10 Nov 2014
I started with the definition
A ={'A1a','A2a','A3a','A1b','A2b','A3b','A1c','A2c','A3c','B1a','B2a','B3a','B1b','B2b','B3b','B1c','B2c','B3c','C1a','C2a','C3a','C1b','C2b','C3b','C1c','C2c','C3c'}
rather than defining your whole dataset.

Sign in to comment.

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!