Search code examples
arraysmatlabdata-structuresparametersdimensions

Comparing different data types with find() in Matlab


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].

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.

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?

Thanks in advance!


Solution

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

    % 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