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 件のコメント
Greg
Greg 2018 年 6 月 4 日
We've all been there.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by