How to Group strings?

조회 수: 1 (최근 30일)
Vihar Chervenkov
Vihar Chervenkov 2013년 4월 12일
Hi Everyone,
How can I group strings based on their values?
Example: a=[1 2 3 4]; b=[0 5 3 5]; c=[9 4 7 7]; d=[9 7 3 1];
I want to make a group of the strings that have value of 3 in their 3rd column. Those will be a,b and d.
Can 1 string perticipate in more than one groups? For example Group 1 is the already mentioned one, and Group 2 would be made of the strings that have value of 9 in the 1st column. Those will be strings c and d.
Cheers, Vihar
  댓글 수: 1
Jan
Jan 2013년 4월 12일
편집: Jan 2013년 4월 12일
"String" means a [1 x N] vector of type char in Matlab. Using the correct terms is essential when discussing about a programming language.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2013년 4월 12일
Storing the data in different variables is not efficient. This would be much faster and easier to expand:
Data = [1 2 3 4; ...
0 5 3 5; ...
9 4 7 7; ...
9 7 3 1];
Group{1} = find(Data(:, 3) == 3);
Group{2} = find(Data(:, 1) == 9);
And even the outputs should not be numbered, but collected in a cell array.

추가 답변 (1개)

Yao Li
Yao Li 2013년 4월 12일
a(1,1:4)=[1 2 3 4];
a(2,1:4)=[0 5 3 5];
a(3,1:4)=[9 4 7 7];
a(4,1:4)=[9 7 3 1];
j=1;
k=1;
for i=1:4
if((find(a(i,1:4)==3)==3)+(find(a(i,1:4)==9)==1)==2)
group_1(j,1:4)=a(i,1:4);
j=j+1;
group_2(k,1:4)=a(i,1:4);
k=k+1;
else
if(find(a(i,1:4)==3)==3)
group_1(j,1:4)=a(i,1:4);
j=j+1;
elseif(find(a(i,1:4)==9)==1)
group_2(k,1:4)=a(i,1:4);
k=k+1;
else
end
end
end

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by