How to find unique elements in a cell array

4 次查看(过去 30 天)
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);

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2011-11-6
[a b c] = unique(sortedinfo(:,2));
[id,id] = sort(c);
out = mat2cell(sortedinfo(id,:),histc(c,1:max(c)),size(sortedinfo,2));
  1 个评论
Matthew
Matthew 2011-11-7
Would you mind explaining what exactly is going on with the 2nd and 3rd lines of code?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by