0

我正在尝试使用evalMATLAB 中的函数创建多个参数数组

data本质上,我有一个大数据集(就目前而言,我正在使用:

variablename = ['a' num2str(academy) '_s' num2str(year) '_g' num2str(gender)];

        %loop through all people, if match various classifications, write to variablename
        for row = 1:totalrows;
            if data(row,2) == academy;
                if data(row,1) == year;
                    if data(row,70) == gender;
                         eval([variablename ' = [ data(row,8) data(row,9) data(row,73) data(row,76) data(row,77) data(row,78) data(row,79) ]; ' ]); % ; supresses output (i.e. stop it showing value of each variable                     


                    end%gender if
                end%year if
            end %academy if
        end %row loop

这工作得相当好,除了每次我得到与所有if语句匹配的第二条记录时,它都会覆盖第一组数据。

所以我的问题是,如何指定eval要写入数据的变量的行?

提前致谢

4

2 回答 2

0

而不是使用 EVAL,考虑这个:

data = [...];           %# some big matrix, rows are records, columns are fields

year = 2012;
gender = 2;             %# female
academy = 5;            %# corresponds to some meaningful value

cols = [8 9 73 76:79];  %# list of columns to select

%# get all rows matching the above conditions
idx = ( data(:,1)==year && data(:,70)==gender && data(:,2)==academy );
X = data(idx,cols);

如果您正在执行这种类型的多个查询,也许您可​​以将结果存储在一个单元格数组中,并保留一个单独的矩阵来存储查询参数,例如:

map(1,:) = [2012 2 5];  %# for the above query, with X{1} containing the data
map(2,:) = [2003 1 4];  %# for another query, with X{2} for corresponding data
于 2012-05-28T06:31:31.067 回答
0

在回复的提示下,我检查了格式并且以下工作

 eval([variablename '(row,:) = [ data(row,8) data(row,9) data(row,73) data(row,76) data(row,77) data(row,78) data(row,79) ]; ' ]); % ; supresses output (i.e. stop it showing value of each variable  
于 2012-05-29T03:48:05.450 回答