필터 지우기
필터 지우기

Manipolation of Cell Arrays.

조회 수: 1 (최근 30일)
Maurizio
Maurizio 2011년 12월 19일
I have {A}=<250x22>cell. thanks to another question I'm able to know the pairs of two colums of {A} for example I have just these three combination :{a}&{b} or {b}&{z} or {r}&{t}. What I would like to create three different cell arrays based on the three combination.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 12월 19일
A={'a','b',1 2 3;
'a','b',3 4 5;
'b','z',3 4 5;
'b','z',4 5 6;
'r','t',5 6 7;
'r','t',6 7 8};
UniqComb={'a','b';'b','z';'r','t'};
N=size(UniqComb,1);
Groups=cell(N,1);
for k=1:N
index=and(strcmp(A(:,1),UniqComb(k,1)),strcmp(A(:,2),UniqComb(k,2)));
Groups{k}=A(index,:);
end
>> Groups{1}
Groups{2}
Groups{3}
ans =
'a' 'b' [1] [2] [3]
'a' 'b' [3] [4] [5]
ans =
'b' 'z' [3] [4] [5]
'b' 'z' [4] [5] [6]
ans =
'r' 't' [5] [6] [7]
'r' 't' [6] [7] [8]
  댓글 수: 8
Maurizio
Maurizio 2011년 12월 20일
@ Fangjun... is it possible to create a for loop that for each Groups{} sum all the number that I have only on the column 3 and 4?
Fangjun Jiang
Fangjun Jiang 2011년 12월 20일
>> sum([Groups{k}{:,3}])
ans =
11

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 19일
a_1 = strcmp('a', A(:,1));
b_1 = strcmp('b', A(:,1));
b_2 = strcmp('b', A(:,2));
r_1 = strcmp('r', A(:,1));
t_2 = strcmp('t', A(:,2));
mab = A(a_1 & b_2, :);
mbz = A(b_1 & z_2, :);
mrt = A(r_1 & t_2, :);
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 12월 19일
Walter, what about http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F ?
Walter Roberson
Walter Roberson 2011년 12월 19일
No loop variables were harmed in the making of this code snippet.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by