matlab - Logical index of structure with various dimensioned fields -


lets have structure this:

s.index = 1:10; s.testmatrix = zeros(3,3,10); x = 1:10     s.testmatrix(:,:,x) = magic(3) + x; end s.other = reshape(0:39, 4, 10); 

it contains 1x10 vector, 3x3x10 multi-paged array , 4x10 matrix. want select entries corresponding indices between 2 , 8. mask = s.index > 2 & s.index < 8;

i tried structfun(@(x) x(mask), s, 'uniformoutput', 0); first correctly worked vector, makes perfect sense. figured needed expand mask. did this.

test = structfun(@(x) x(repmat(mask, size(x, ndims(x) - 1), 1)), s, 'uniformoutput',0); 

the expanded mask correct matrix not multi-paged array. , 2d matrix flattened vector.

if going index these elements individually this:

s2.index = s.index(mask); s2.other = s.other(:,mask); s2.testmatrix = s.testmatrix(:,:,mask); 

my use case hundreds of structures each 20+ fields. how script indexing? exact problem occurs limited structure 1xn vectors, 3xn , 4xn matrices , 3x3xn arrays. mask constructed based on 1 of vectors representing time. field names constant each structure brute force thing , type in commands , run function, i'm looking intelligent way index it.

update: here looks promising.

fn = fieldnames(s); x = 1:length(fn)     extradim = repmat({':'}, 1, ndims(s.(fn{x})) - 1);     s2.(fn{x}) = s.(fn{x})(extradim{:}, mask); end 

you can exploit fact the string ':' can used index instead of :, , build comma-separated list of string repeated appropriate number of times each field:

s = {':',':'}; % auxilary cell array generate comma-separated list s2 = structfun(@(f) f(s{1:ndims(f)-1}, mask), s, 'uniformoutput', false); 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -