在MATLAB中用结构写一个循环?

时间:2012-02-23 05:08:55

标签: matlab

我正在尝试编写一个循环,这个循环将使用userIdx中的每个数字,然后进入结构magStruct,并且每次通过循环获取该结构内的索引矩阵并将所有数字连接成一个矩阵。然后我想取出所有重复的数字副本,这样我就可以从我指定的数字中将所需数字中的所有数字的唯一数字矩阵留给函数。

magStruct是一个结构,索引是双精度矩阵。

我已经编写了循环和数字的连接,但循环给了我一个错误,即使那时我认为矩阵的每个元素都是矩阵本身而不仅仅是数字。另外,删除矩阵中重复数字的功能是什么?感谢。

function coverage = getCoverage( userIdx, magStruct)
% getCoverage returns the list of magazines for users in input vector
% Input Arguments:
% userIdx = an row vector with user indexes
% magStruct = the magazineData structure
% Output Arguments:
% coverage = a list of the unique magazines that users in userIdx subscribe to

a = 0;
for k = userIdx
    mags(a) = magStruct(k).indexes;
    a = a + 1;
end

1 个答案:

答案 0 :(得分:1)

magStruct中索引矩阵的大小是多少?

如果它们是具有相同列数的数字矩阵,则可以将它们与

连接起来
mag = cat(1,magStruct(userIdx).indexes);

对于相同的行数,只需将1更改为2

删除重复使用UNIQUE功能:

magunq = unique(mag);

如果索引矩阵的大小不同,则会更复杂一些。您可以先将它们转换为列向量,然后以相同的方式连接:

temp = arrayfun(@(x) x.a(:), magStuct, 'uniformoutput',0);
mag = cat(1,temp{userIdx})
相关问题