Reorganizing cell array according to column
Mostrar comentarios más antiguos
I have a cell-array 40x6, the values in the 6th column are the numbers 1 and 2 which correspond to two different conditions in my study. The rows of the cell array are currently organized in a way that all of the 2 conditions rows are first and then all of the 1 condition rows.
This is a short excerpt of what the 6th column looks like now.
[2 2 2 2 2 1 1 1 1 1]
I want to re-sort this column so that I create a column with the condtions alternating [1 2 1 2 1 2], and then sort the rows according to this new order. Is there a way to do this with the sort function?
4 comentarios
Stephan
el 26 de Feb. de 2019
Please attach your data
Danna Pinto
el 26 de Feb. de 2019
Danna Pinto
el 26 de Feb. de 2019
Respuesta aceptada
Más respuestas (1)
Mani Teja
el 26 de Feb. de 2019
0 votos
Hello Danna,
The below code can be used if you are looking for other ways apart from using the "Sort" function:
% Converting from cell to mat format
C = cell2mat(Place_Your_Cell_Array_Here);
% Use this matrix to visualize the changes made to the original matrix
Compare_C = C;
% Divide the given matrix into half matrices (Based on 1s and 2s in 6th Column)
D_1 = C(1:1:20,:);
D_2 = C(21:1:40,:);
% Sorting of C Matrix
for i = 1:2:40
C(i,:) = D_2((i+1)/2,:);
C(i+1,:) = D_1((i+1)/2,:);
end
% Converting from mat to cell format
Your_Sorted_Cell_Array = num2cell(C);
% Alternatively the matrix can be sorted by identifying 1s and 2s in 6th
% Column using if loops and a counter for a particular order
Hope this helps !
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
