我现在正试图使用以下代码在matlab中对一个1 x N的单元格数组进行内容洗牌:
shuffledframes = frames{randperm(NumberOfFrames)};
frames=shuffledframes;
%printing cell array contents
for i=1:NumberOfFrames
frames(i)
end
然而,框架内容似乎并没有被 suffle...
代码中是否存在我看不到的错误?
你需要替换
shuffledframes = frames{randperm(NumberOfFrames)};
通过以下任一方式:
Standard, recommended way:
shuffledframes = frames(randperm(NumberOfFrames));
More complicated alternative using lists:
[frames{:}] = frames{randperm(NumberOfFrames)};
frames{randperm(NumberOfFrames)}
给出了一个由数字组成的逗号分隔列表。Matlab只取该列表的第一个数字并将其赋值给shuffledframes
。frames(randperm(NumberOfFrames))
使用索引向量索引原始单元格数组以生成新的单元格数组,这正是您想要的。frames {:}
的每个值填充到列表frames {randperm(NumberOfFrames)}
的每个值中。>> frames = {1,2,3,4};
>> NumberOfFrames = 4;
>> frames{randperm(NumberOfFrames)} %// Your code. Gives a list of values.
ans =
3
ans =
4
ans =
2
ans =
1
>> frames(randperm(NumberOfFrames)) %// Approach 1. Gives cell array.
ans =
[3] [1] [4] [2]
frames {:}
来显示所有值,而不是使用循环。 - Dennis Jaheruddin