How to find unique elements in a cell array

조회 수: 4 (최근 30일)
Matthew
Matthew 2011년 11월 6일
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