Problem Diagnosing Adding Vectors to Cell Array

조회 수: 1 (최근 30일)
William_M
William_M 2018년 6월 3일
댓글: Greg 2018년 6월 4일
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.

채택된 답변

Greg
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
  댓글 수: 2
William_M
William_M 2018년 6월 4일
Thanks, it wasn't quite that, I should've put length®-1, can't believe I missed that haha!
Greg
Greg 2018년 6월 4일
We've all been there.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by