如何一次将索引应用于许多不同的变量?

时间:2015-05-22 14:49:08

标签: matlab indexing

我有几个不同数据类型的向量,大小相同。具体来说,我将Datetimes作为双打,字符串等的日期戳。我希望快速轻松地删除所有周末,以便我从日期时间创建和索引。我现在如何将此索引应用于我的所有变量?

目前我(有一小部分),

Date=Date(idx);
Meter=Meter(idx); 
Model=Model(idx);
.
.
.

是否存在某些现有功能,例如,

[Date,Meter,Model,...]=fnc(idx,Date,Meter,Model,...);
我很想写自己的,应该很容易,但如果有其他一些简单或有效的替代方案,我不想这样做。

3 个答案:

答案 0 :(得分:4)

@Luis Mendo 指出的使用单元格乐趣的另一种方法是使用structfun - 这样就可以保留每个数组的变量名称。

您需要在结构中包含您的变量才能使其正常工作:

myStruct.Date  = Data;
myStruct.Meter = Meter;
myStruct.Model = Model;
subStruct = structfun ( @(x) x(idx), myStruct, 'UniformOutput', false )

答案 1 :(得分:3)

你可以这样:

t = cellfun(@(x) x(idx), {Date, Meter, Model}, 'uniformoutput', 0);
[Date, Meter, Model] = deal(t{:});

在Matlab的最新版本中,您可以omit deal,因此第二行变为:

[Date, Meter, Model] = t{:};

如果您使用单元格数组而不是单独的变量,那么每个单元格都包含一个变量会更容易。在这种情况下,您只需使用

myCell = cellfun(@(x) x(idx), myCell, 'uniformoutput', 0);

答案 2 :(得分:1)

您可以将该函数定义为匿名函数,如下所示:

>> A=rand(1,3)
A =
    0.9649    0.1576    0.9706
>> B={'a' 'b' 'c'}
B = 
    'a'    'b'    'c'
>> [x,y]=f(2,A,B)
x =
    0.1576
y = 
    'b'

现在

class Exibitions {

    String name
    String location
    String description
    Date dateStart
    Date dateEnd


    byte[] flier

    static mapping = {
        flier sqlType:'longblob'      //use mysql
    }

    static constraints = {
        description nullable: true
        flier nullable: true
    }
}
相关问题