Problem Diagnosing Adding Vectors to Cell Array

1 view (last 30 days)
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.

Accepted Answer

Greg
Greg on 4 Jun 2018
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 Comments
William_M
William_M on 4 Jun 2018
Thanks, it wasn't quite that, I should've put length®-1, can't believe I missed that haha!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!