how to apply column wise operation on each matrix in cell

3 次查看(过去 30 天)
I want to apply a condition of cell array allCells_array which contains matrices of 7x3 size.I want to check that if sum of each column of matrix is less than equal to 3 then do multiplication else go to next matrix in cell how to define this function and where to put END of IF in the given code?
for m=1:c2
for n=1:numel(allCells_array)
*here to put IF STATEMENT*
movement=(all_comb_of_routes{m})*allCells_array{n};
movement=movement>0;
total_movement=sum((sum(movement,2))-1);
count=count+1;
total_all(count)=total_movement;
end
end
complete code:
%%///FOR ALL COMBINATIONS OF ROUTES(64X1806)
clear all;
clc;
tic;
no_of_machines=7;
no_of_cells=3;
No_of_Parts = 6;
%/// STEP 1: MAKING 64 COMBINATIONS OF ALL PARTS ROUTING
P1=[1 0 0 1 0 1 1;1 1 0 0 1 0 1];% AVAILABLE ROUTES FOR PART 1
P2=[0 1 1 1 0 0 1;1 0 1 0 1 1 0];% AVAILABLE ROUTES FOR PART
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];%//
% GENERATING ALL POSSIBLE COMBINATIONS OF 12 ROUTES
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);
all_comb_of_routes = cell(1,n);
for k=1:n
all_comb_of_routes{k} = P(c+a(k,:),:);
end
%//STEP 2:GENERATING ALL CELLS COMBINATIONS FOR MACHINES
CELL = zeros(no_of_cells^no_of_machines,no_of_machines);
t = 0;
for k = 0:(no_of_cells^no_of_machines)-1 %k=(2187-1=2186)
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);% all possible combinations
%//GENERATING MACHINE CELL MATRICES FROM ABOVE 1806 COMBINATIONS
[r1,c1]=size(combination_array);
for l=1:r1 %(l)= 1806 (no.of combinations of machines and cells)
R=1:numel(combination_array{l});
Z = zeros(R(end),max(combination_array{l}));
Z(sub2ind(size(Z),R,combination_array{l})) = 1;
allCells_array{l}=Z; % machine cell matrix array of all combinations
end
[r2,c2]=size(all_comb_of_routes);
count=0;
for m=1:c2
for n=1:numel(allCells_array)
movement=(all_comb_of_routes{m})*allCells_array{n};
movement=movement>0;
total_movement=sum((sum(movement,2))-1);
count=count+1;
total_all(count)=total_movement;
end
end
minValue=min(total_all)
[all_comb_of_routes(m) combination_array(n) allCells_array(n) minValue]
toc;

采纳的回答

Image Analyst
Image Analyst 2017-1-14
Not quite sure what the loop over n is for, but from your words, it sounds like you want to do something like this:
for m = 1 : length(all_comb_of_routes)
% Extract the 7-by-3 matrix from inside the cell
thisCellContents = all_comb_of_routes{m}
% Sum if vertically.
columnSums = sum(thisCellContents, 1);
% See if each column sums to less than 3
% and multiply by some number.
if all(columnSums < 3)
% All 3 sums are less than 3.
thisCellContents = thisCellContents * allCells_array{n};
% Put new result back into the cell
all_comb_of_routes{m} = thisCellContents;
else
% At least one of the columns sums to 3 or more.
% Nothing to do, just skip to the end of the loop
continue;
end
end
  2 个评论
summyia qamar
summyia qamar 2017-1-14
i've given the complete code above..
  1. I want to extract element of cell named as allCells_array the element is 7x3 matrix.
2.check if each column sum <3, if yes then proceed to next step i-e
movement=(all_comb_of_routes{m})*allCells_array{n};
else move to next element
Image Analyst
Image Analyst 2017-1-14
编辑:Image Analyst 2017-1-14
For #1, I did that:
% Extract the 7-by-3 matrix from inside the cell
thisCellContents = all_comb_of_routes{m}
That will be a 7x3 numerical array, not a cell anymore.
For #2, you need to use the all() function on the column sums, as I showed in my example.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by