Manipolation of Cell Arrays.

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.

 Accepted Answer

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 Comments

Fangjun I have 250x22cell. The combination are only for the two colums. What I would like to do is to create based on these three configuration 3 cell array indipendent where on each cell array there are only the data of the correct configuration
That is what the code is for. All the data in Groups{1} will have first column as 'a' and second column as 'b'. All the data in Groups{2} will have first column as 'b' and second column as 'z'. And etc.
may be I'm not able to explain.
I analyse the colums 2 and 3 of {A} ({A}=250x22 cell) and I have three different combination of these 250 rows.
The combination on all the 250 rows (on the two colums) are 'a' with 'b' or 'b' with 'z' or 'r' with 't'. What I would like to do is read all the 250 rows and create three different structure based on the three configuaration..Example
first combination 240 times.
second combination 5 times.
third combiantion 5 times.
The combination can be in disorder.
What I like to do is create 3 different structure each one with all
the colums (22) of the correct combination and with the example above will be for example is 240x20 cell, 5x20 cell and 5x20cell.
Besides the column 1,2 and column 2,3 difference, that is the exact code. Why don't you try it on your data set A?
See update with an example.
I'm not certain but possibly Maurizio would like the order to be irrelevant; i.e., to also allow 'b' 'a', 'z' 'b' and 't' 'r'
@ 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?
>> sum([Groups{k}{:,3}])
ans =
11

Sign in to comment.

More Answers (1)

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 Comments

Walter, what about http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F ?
No loop variables were harmed in the making of this code snippet.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!