interpolate data in cell array
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jessica Smith
am 7 Jul. 2016
Kommentiert: Jessica Smith
am 7 Jul. 2016
I have 2 cell arrays both 5x1 cell. The first cell array contains A=40x1 double and the second B=40x11. Data in A{1,1} is related to B{1,1}. What I would like to do is use the function interp1 on each array of A and interpolate the corresponding data in B in the same manner. I have used the following code with no error before the data was contained in an array (I should say I need to repeat the interpolate on each of 11 columns of cell array B):
angle = (0:3:360)';
a = data(:,1);
b(:,1)=[];
b = interp1(a,b,angle);
My initial thought is to somehow use a for loop to go through each array and repeat the function, however I am unsure how to use the function on data presented this way.
Thanks, Jess
0 Kommentare
Akzeptierte Antwort
Guillaume
am 7 Jul. 2016
Possibly, this will do what you want:
%A and B: cell arrays of matrices. Must have the same size
%Matrices in corresponding cells in A and B must have the same number of rows
angle = (0:3:360)';
newb = cellfun(@(a,b) interp1(a, b(:, 2:end)), angle), A, B, 'UniformOutput', false);
I'm simply using cellfun to iterate over all the cells of A and B simultaneously. cellfun calls an anonymous function which is just a rewording of the code you've provided.
5 Kommentare
Guillaume
am 7 Jul. 2016
Bearbeitet: Guillaume
am 7 Jul. 2016
Yes, as Walter said, the a and b are just the variable names of the arguments of the anonymous function. Just as any function, you can use whatever you want as long as you use the same names in the body of the function/anonymous function (i.e. the call to interp1 in this case).
What is important are the inputs to cellfun, which in this case are A and B (for lack of a better name). If these two are not cell arrays, then of course cellfun is going to complain.
The for loop equivalent of the cellfun expression I've written would be:
assert(iscell(A), 'Input #1 expected to be a cell array');
assert(iscell(B), 'Input #2 expected to be a cell array');
assert(size(A) == size(B), 'Inputs expected to be the same size');
newb = cell(size(A));
for celliter = 1:numel(A)
a = A{celliter};
b = B{celliter};
newb{celliter} = interp1(a, b(:, 2:end)), angle);
end
As you can see cellfun is a lot more succinct!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!