How to find unique elements in a cell array
4 views (last 30 days)
Show older comments
I am trying to search through a cell array that holds both strings and integers. This cell array is sorted via the third column. In the second column is a list of majors for the students that where given by the user. I need to search through this second column and pull out only the unique majors and put them into a list to be used with listdlg, that then upon selecting the major from the list shows the user which people in the cell array share the selected major. The code of what I have can be found below.
information = {};
for i = 1:5
prompt_1 = {'Name','Major','Grad Year','Lucky Number(s)(use [])'};
dlg_title = 'User information list and compare';
num_lines = 1;
def_1 = {'John Doe','AE','2015','[1 33 7]'};
answer = inputdlg(prompt_1,dlg_title,num_lines,def_1);
information{i,1} = answer{1,1}; %#ok<*SAGROW>
information{i,2} = answer{2,1};
information{i,3} = answer{3,1};
information{i,4} = answer{4,1};
end
%sorted by graduation year information array
sortedinfo = sortrows(information,3);
0 Comments
Answers (1)
Andrei Bobrov
on 6 Nov 2011
[a b c] = unique(sortedinfo(:,2));
[id,id] = sort(c);
out = mat2cell(sortedinfo(id,:),histc(c,1:max(c)),size(sortedinfo,2));
See Also
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!