在Matlab中使用find()比较不同的数据类型

时间:2022-11-12 16:39:32

I am running experiments in Matlab with different parameter sets and would like to store some scalar results such as mean, variance etc. The parameters however do not all have the same type, and even within one type of parameter not all entries are of the same dimensions. For example I have one parameter cellSize which can be scalar i.e. 4 or a vector of multiple values i.e. [4, 6, 10].

我在Matlab中运行不同参数集的实验,并希望存储一些标量结果,如均值,方差等。但是参数并不都具有相同的类型,即使在一种类型的参数中,并非所有条目都是相同的尺寸。例如,我有一个参数cellSize,它可以是标量,即4或多个值的矢量,即[4,6,10]。

I found that I can store the results using a struct array s like this (simplified version with only one parameter):

我发现我可以使用像这样的结构数组存储结果(只有一个参数的简化版本):

s = struct('cellSize', []);
s(1).cellSize = 4;
s(2).cellSize = [4, 6, 10];

but my problem is that now I can't search for rows matching a specific parameter set using for example find(s.cellSize = [4, 6, 10]) because the matrix dimensions do not agree.

但我的问题是,现在我无法使用例如find(s.cellSize = [4,6,10])搜索匹配特定参数集的行,因为矩阵维度不一致。

I would like to search in the data this way such that I can check wether an experiment with the current parameter set is already present in the results data.

我想以这种方式搜索数据,以便我可以检查结果数据中是否已存在当前参数集的实验。

Is there a way to do this with struct arrays or would it be best to implement something with a for-loop myself? Or else, is there a better suited data structure that I can use for this kind of data?

有没有办法用struct数组做这个或者最好用自己的for循环实现一些东西?或者,是否有更适合我可用于此类数据的数据结构?

Thanks in advance!

提前致谢!

1 个解决方案

#1


1  

you can convert the specific strut field into cell and use cellfun to search the matching parameters in this cell array:

您可以将特定的strut字段转换为单元格,并使用cellfun搜索此单元格数组中的匹配参数:

% generate struct
s = struct('cellSize', []);
s(1).cellSize = 4;
s(2).cellSize = [4, 6, 10];
s(3).cellSize = [5, 5];
% query parameters
q = [4, 6, 10];
% convert field to cell array
c = {s.cellSize};
% find index
idx = find( cellfun(@(x) isequal(x,q),c) ) % idx = 2

#1


1  

you can convert the specific strut field into cell and use cellfun to search the matching parameters in this cell array:

您可以将特定的strut字段转换为单元格,并使用cellfun搜索此单元格数组中的匹配参数:

% generate struct
s = struct('cellSize', []);
s(1).cellSize = 4;
s(2).cellSize = [4, 6, 10];
s(3).cellSize = [5, 5];
% query parameters
q = [4, 6, 10];
% convert field to cell array
c = {s.cellSize};
% find index
idx = find( cellfun(@(x) isequal(x,q),c) ) % idx = 2