Problem Diagnosing Adding Vectors to Cell Array
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm having trouble with adding zero row vectors to a cell array and can't figure out why: (Only the last two for loops are relevant for the problem)
function C = SplitS(n)
%Split a symetric group n, into each of it's m-cycles.
Q=0;
R=zeros(1,n);
%Create a row vector storing each of the cumulative sum of the number of m (column) cycles
for i = 1:n
r=factorial(n)/(i*factorial(n-i));
Q=Q+r;
R(i)=Q-n+1; %1st term is always n and should be 1
end
Q=Q-n+1;
C=cell(1,Q);
C{1}='id';
for j=1:length(Q)-1
for k = R(j)+1:R(j+1)
C{k}=zeros(1,j+1);
end
end
end
I'm pretty sure that there's not a problem with indexing, but with adding the zero vectors to the empty cell array. For example, if I input n=3:
>> SplitS(3)
ans =
1×6 cell array
Columns 1 through 5
{'id'} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
Column 6
{0×0 double}
>> ans{2}
ans =
[]
However, what I want to return in this case is a cell array with 1*2 vectors of zeros in columns 2 to 4 and 1*3 zero vectors in rows 5 and 6. Any help much appreciated.
댓글 수: 0
채택된 답변
Greg
2018년 6월 4일
Can't track for sure, but it looks like Q is a scalar. I think you want to replace:
for j = 1:length(Q)-1 % length(Q) = 1, so 1:1-1 is a little silly...
with
for j = 1:Q-1
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!