Question

I am having a large struct array with following fields:

x
y
z_value
level

and I need to perform a search based on the condition like z_value == 10 && level = 5.

Currently my code loops from the first element to the last checking all the struct values. Since the array is large its taking ~80secs to complete.

Is there any other alternative(other than by using binary search,heap etc) to make this fast by exploiting in build features of matlab??

Was it helpful?

Solution

Assuming the struct to have only double data, this code would get all those indices where the mentioned condition is satisfied -

names = fieldnames(s1) %%//s1 is your input struct

%%// matdata = cell2mat(squeeze(struct2cell(s1)))' %%//' Approach -1
matdata = reshape(struct2array(s1),numel(names),[])' %%//' Approach -2
data1 = matdata(:,[find(strcmp(names,'z_value')) find(strcmp(names,'level'))])
index = find(ismember(data1,[10 5],'rows')) %%// indices where condition is met
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top