interpolate data in cell array

조회 수: 17 (최근 30일)
Jessica Smith
Jessica Smith 2016년 7월 7일
댓글: Jessica Smith 2016년 7월 7일
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

채택된 답변

Guillaume
Guillaume 2016년 7월 7일
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
Guillaume
Guillaume 2016년 7월 7일
편집: Guillaume 2016년 7월 7일
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!
Jessica Smith
Jessica Smith 2016년 7월 7일
Ah I understand, my error, apologies. I will see where I have gone wrong and yes cell fun is a lot more concise. Thank you for your inputs!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by