Matlab存储矩阵在单元格中

时间:2016-05-18 18:26:21

标签: arrays matlab

我有一个函数可以返回我想要存储在单元格中的信息矩阵,但无论我如何尝试这种情况,它都会一直给我各种错误或结果不正确,我的最新尝试如下所示:

bbag=[]
for j=3:100
    bag=rand(randi([1 5]),randi([1 5]))%stand in for more complex function that normally returns between 1 and 4 
    [dontcare,y] = size(bag);
    tbag={bag(1,1),bag(2,1),bag(3,1),bag(4,1),bag(4,1)}
    for i=2:y
        tbag=[tbag,{bag(1,i),bag(2,i),bag(3,i),bag(4,i)}]; %some kind of loop is probably required here 
    end
    bbag=vertcat(bbag,tbag)
    labels(i) = 1;
end

但是当数据包含除4个数据列以外的任何内容时,这无法处理,如果它只是设法将所有数据附加到同一行而不是将其放在自己的单元格中,任何想法如何做到这一点,通过最后我可以要求bbag(2,3),然后返回一个包含1到5个值的单元格?如果我将大小固定为4然后我得到一个98乘17的单元块(而不是98x4x4,我期望)。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

class Foo(models.Model):
    parent = models.ForeignKey('Foo', blank=True, null=True)

    def descendants(self):
        """
        When callers don't need the complete list (eg, checking if any dependent is 
        viewable by any user), we run fewer queries by only going into the dependent 
        hierarchy as much as necessary. Returns a generator of querysets of Foo objects.
        """
        immediate_descendants = Foo.objects.filter(parent=self)
        yield immediate_descendants
        for x in immediate_descendants:
            for y in x.descendants():
                yield y

    def obj_or_descendant_has_perm(self, perm_code):
        perm_id = Permission.objects.get(codename=perm_code).id

        if FooUserObjectPermission.objects.filter(permission_id=perm_id,
                                                  content_object=self).exists()
            return True
        if FooGroupObjectPermission.objects.filter(permission_id=perm_id,
                                                   content_object=self).exists()
            return True

        for gen in self.descendants():
            if FooUserObjectPermission.objects.filter(permission_id=perm_id,
                                                      content_object__in=gen).exists()
                return True
            if FooGroupObjectPermission.objects.filter(permission_id=perm_id,
                                                       content_object__in=gen).exists()
                return True

        return False

您创建一个随机大小的随机矩阵(即行和列的随机数)。我不知道错误是否与单元格数组创建有关;他们与您访问的事实有关来自bag=rand(randi([1 5]),randi([1 5])); 的第3行,但未确定您实际中有 3行。

另外,检查bag的文档是否将矩阵拆分为异构大小矩阵的单元格数组(如果这确实是您要查找的内容)。