How to make an anonymous function for variable amount of input data
Ältere Kommentare anzeigen
Hi all,
I'm trying to generate an anonymous function where I pass in an array of structures and convert it to a structure of arrays (possibly offsetting the indices for each of the inputs). I've been doing it so far using an anonymous function, but I'm trying to figure out how to generalize it.
Here's what I've got so far:
Sync = @(x,fna) [x(1).(fna)(shape_params.indices{1}),...
x(2).(fna)(shape_params.indices{2})];
So when I call it I give it the array of structures (x), and the field name (fna) that I want to create the output array of. shape_param.indices is a cell array that tells me which entries I want from each of the entries in the array of structs.
For example:
XX(1).a = (1:10)';
XX(2).a = (2:11)';
shape_params.indices{1} = (2:5)';
shape_params.indices{2} = (1:4)';
YY.a = Sync(XX,'a')
should give me:
YY.a = [2,2;3,3;4,4;5,5];
I can define Sync to accomodate the number of entries in XX by just creating a switch. But now I need to generalize it, since I don't want to make a switch statement that has up to 16 varieties.
Any suggestions on how I can do it, for an unknown number of vectors that I'm combining?
Thanks, Dan
1 Kommentar
Akzeptierte Antwort
Weitere Antworten (1)
Tucker McClure
am 8 Feb. 2013
Not sure I exactly understand you, Dan, but is this what you're trying to do? This is the anonymous function version. You might want to consider making a private method instead, but if it needs to be anonymous, then this seems to get the job done.
f = @(objects, indices) ... % Function header
arrayfun(@(k) objects(k).field(indices{k}), ... % Get indices of obj(k)
1:length(objects), ... % for k = 1:n.
'UniformOutput', false); % Output in cells.
my_struct_array(1).field = (1:10)';
my_struct_array(2).field = (2:2:20)';
my_indices = {1:5, 6:10};
f(my_struct_array, my_indices)
Also, if the indices are guaranteed to be the same length, then you could output to a matrix instead of a cell array by adding a cell2mat(...) around the arrayfun(...).
Hope that helps.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!