what is wrong?
조회 수: 2 (최근 30일)
이전 댓글 표시
why it show error
??? Error using ==> cat CAT arguments dimensions are not consistent.
Error in ==> @(i)cat(3,randperm(m)',[11*ones(N(1,i),1);22*ones(N(2,i),1);33*ones(N(3,i),1);44*ones(N(4,i),1)])
m=22;
AA = [m 7];
N=[11,9,11,10,10,9,9;7,7,7,7,7,7,7;4,5,4,4,4,5,5;1,1,1,1,1,1,1];
B = cell2mat(arrayfun(@(i)cat(3,randperm(m)',[11*ones(N(1,i),1);22*ones(N(2,i),1);33*ones(N(3,i),1);44*ones(N(4,i),1)]),1:size(N,2),'un',0));
x = B(sub2ind(szAA,B(:,:,1),cumsum(ones(AA),2))+prod(AA));
i cant understand what the problem,can someone help me?
댓글 수: 1
채택된 답변
Andrei Bobrov
2011년 5월 12일
Hi Sahab! More variant solving your problem
m=22;
AA = [m 7];
N=[11,9,11,10,10,9,9;7,7,7,7,7,7,7;4,5,4,4,4,5,5;1,1,1,1,1,1,1];
t=sum(N)~=AA(1);
if any(t), I1 = randi(size(N,1)-1); N(I1,:) = N(I1,:)-t; end
x = zeros(AA);
xx = 11*(1:4)';
for j = 1:size(N,2)
x(randperm(AA(1)),j) = ...
cell2mat(arrayfun(@(i)xx(i)*ones(N(i,j),1),1:length(xx),'un',0).');
end
추가 답변 (2개)
Matt Fig
2011년 5월 11일
The problem is that in your first N, all of the values sum to m:
m=22;
N=[8,11,18,7,9,8,13;6,2,1,5,3,6,1;4,3,2,5,5,4,6;4,6,1,5,5,4,2];
all(sum(N)==m)
and the second does not.
N=[11,9,11,10,10,9,9;7,7,7,7,7,7,7;4,5,4,4,4,5,5;1,1,1,1,1,1,1];
all(sum(N)==m)
In order to get away from the CAT error, you need all(sum(N)==m) to return true.
Andy
2011년 5월 11일
When you cat along the third dimension, the first two dimensions must match up. Let's look at what your cat-ing. First, you have randperm(m)', a column vector of size [m 1], where m is always 22. Then you have
[11*ones(N(1,i),1);22*ones(N(2,i),1);33*ones(N(3,i),1);44*ones(N(4,i),1)]
For, say, i=1, this has size [N(1,1)+N(2,1)+N(3,1)+N(4,1), 1]. This is a column vector of length 23 (and the length will be different depending on i). So the dimensions don't match up correctly.
What are you really trying to do with this code?
댓글 수: 2
Andy
2011년 5월 11일
Matt Fig responded correctly in his answer below, except I think he intended:
all(sum(N) == m)
rather than
all(sum(N)) == m
It is either an accident that one of your values of N satisfies this constraint (which MUST be satisfied for the cat to work), or the N which does not work was incorrectly calculated somehow. Again, could you describe what your code is supposed to accomplish? How is N calculated? What are B and x ultimately supposed to hold? Can you give a small example of some input and the desired output?
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!