what is wrong?

조회 수: 2 (최근 30일)
Sahab
Sahab 2011년 5월 11일
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
Sean de Wolski
Sean de Wolski 2011년 5월 11일
I don't know but that equation above is painful on the eyes.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
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
  댓글 수: 1
Sahab
Sahab 2011년 5월 13일
thanks andrei, this is what i need.. :D

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt Fig
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.
  댓글 수: 1
Sahab
Sahab 2011년 5월 13일
thanks for the replied Matt Fig. this help me too,but andrei bobrov solution is what i need.

댓글을 달려면 로그인하십시오.


Andy
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
Sahab
Sahab 2011년 5월 11일
when i change the value of 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]
it show no error..why i cant use 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];
what should i change?
Andy
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by