Info
此问题已关闭。 请重新打开它进行编辑或回答。
why the answer is not showing any matrix after applying IF condition?
1 次查看(过去 30 天)
显示 更早的评论
can anybody please check this code. After running this, I cannot find any cell named allCells_array 1st it generates combinations of 3 cells and 7 machines using this part
no_of_cells=3;
no_of_machines=7;
CELL = zeros(no_of_cells^no_of_machines,no_of_machines);
t = 0;
for l = 0:(no_of_cells^no_of_machines)-1
s = dec2base(l,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);
the transform the above arrays into matrices of binary digits
[r1,c1]=size(combination_array);
for m=1:r1
R=1:numel(combination_array{m});
Z = zeros(R(end),max(combination_array{m}));
Z(sub2ind(size(Z),R,combination_array{m})) = 1;
*here I want that the obtained Z must have all column sums >= 2*
X=sum(Z,2);
if all(X>=2)
allCells_array{m}=X;
end
end
but the result is not showing any allCells_array why is this so?
1 个评论
Adam
2017-1-25
编辑:Adam
2017-1-25
Have you used the debugger? It should be trivial to find the reason why if you just put a breakpoint in. Certainly a lot easier than someone trying to just scan over the code and work out what is what and where.
Put a breakpoint at the start of the 'if' line and take a look at X.
回答(1 个)
dpb
2017-1-25
编辑:dpb
2017-1-27
"... I cannot find any cell named allCells_array"
X=sum(Z,2); % will be column vector of sums
if all(X>=2) % will be T IFF every row in X is >2 which doesn't happen
Think you're looking for
allCells_array{m}=X(X>=2);
probably unless you actually want Z for those rows for which X>1 rather than the sums.
ADDENDUM
Follow-up to comment earlier on the building of the Z array....as I thought, it's clear this can never work as you've written it to return anything as in
Z(sub2ind(size(Z),R,combination_array{m})) = 1;
For the case you have, the index expression reduces to
sub2ind([7,3],[1:7],combination_array{m})
which asks for a set of indices for each of 7 rows with only a single element to go with each from the array. Hence, there can NEVER be a case where the sum of the rows is >1 as there is just one column associated with each row in the array that is going to be set.
As noted there, I still don't follow just what your intent is here precisely; I kinda' grasp it's trying to split up combinations of N things amongst M pools, but don't have the time to try to divine the intent from non-working code with no comments.
But the above is why you don't get any results in the end...and never will with the logic as is.
3 个评论
dpb
2017-1-25
Well, that's precisely what the logical indexing does--question is just what you want stored; the sums or the components of the sums?
dpb
2017-1-25
编辑:dpb
2017-1-25
But, I instrumented your code with a pinpoint diagnostic of
if(any(X)>1),disp(i),end
to get a location to look at what's going on and guess what!!?? No locations showed up. Your logic in building Z doesn't appear to be what you're expecting it to be. I'm not absolutely sure what this is trying to do without more time than have to spend trying to figure it out, but look carefully out how you're building Z; doesn't look to me as though there's ever more than one bit per any line in the array as is...
此问题已关闭。
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!