how to extract matrices from a cell based on column sum of each matrix?

1 次查看(过去 30 天)
I have a cell A which contains 1806 matrices. each matrix is a binary matrix with 7x3 size.. I want to extract only those matrices from A in which sum of each column is greater than equal to 2 for example A contains matrix
A{1}=[1 0 0;
1 0 0;
1 0 0;
1 0 0;
1 0 0;
0 1 0;
0 0 1]
A{2}=[1 0 0
0 0 1
0 1 0
0 0 1
0 1 0
1 0 0
0 0 1]
A{3}=[0 1 0
0 0 1
1 0 0
0 0 1
1 0 0
0 0 1
0 0 1]
A{4}=[0 1 0
0 0 1
1 0 0
0 0 1
0 1 0
1 0 0
0 1 0]
I want to multiply these matrices with another cell of matrices but only those matrices for which sum of each column is >= 2. I got the idea of applying this but how to continue the loop if A{i} is not satifying the condition
for i=1:numel(A)
for =1:numel(B)
if all(sum(A{i})>=2
C{i,j}=bsxfun(@times,A{i},B{j})
end
end
how to store values of C for which A is according to condition.? the complete code is below
tic;
no_of_machines=7;
no_of_cells=3;
No_of_Parts = 6;
move_cost=[0.12;0.12;0.12;0.12;0.12;0.12];
P1=[1 0 0 1 0 1 1;1 1 0 0 1 0 1];
P2=[0 1 1 1 0 0 1;1 0 1 0 1 1 0];
P3=[1 0 0 1 1 0 0;0 1 1 0 0 0 1];
P4=[1 0 0 0 1 0 1;0 1 0 1 0 1 0];
P5=[1 1 0 0 0 1 0;1 1 0 0 1 0 1];
P6=[0 1 0 0 0 1 1;1 1 0 1 0 1 0];
P = [P1;P2;P3;P4;P5;P6];
z = [size(P1,1) size(P2,1) size(P3,1) size(P4,1) size(P5,1) size(P6,1)];
c = [0 cumsum(z(1:end-1))];
a = allcomb(1:z(1),1:z(2),1:z(3),1:z(4),1:z(5),1:z(6));
n = size(a,1);
B = cell(1,n);
for i=1:n
B{i} = P(c+a(i,:),:);
end
t = 0;
for k = 0:(no_of_cells^no_of_machines)-1
s = dec2base(k,no_of_cells,no_of_machines);
if length(unique(s))==no_of_cells
t = t+1;
CELL(t,:) = s-'0'+1;
end
end
CELL = CELL(1:t,:);
combination_array=num2cell(CELL,2);
for c=1:numel(combination_array )
R=1:numel(combination_array{c});
Z = zeros(R(end),max(combination_array{c}));
Z(sub2ind(size(Z),R,combination_array{c})) = 1;
A{c}=Z;
end
I want to multiply A and B where B is 1x64 cell and A is 1X1806 cell. this gives total of 64x1806 cell.but I want to multiply B with only those A which fulfill the condition of column sum.

回答(1 个)

Walter Roberson
Walter Roberson 2017-1-25
mask = cellfun(@(M) all(sum(M,1)>=2), A);
output = cellfun(@times, A(mask), B(mask), 'uniform', 0);

类别

Help CenterFile Exchange 中查找有关 Matrix Operations and Transformations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by