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

 Respuesta aceptada

Guillaume
Guillaume el 7 de Jul. de 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 comentarios

Thank you, cellfun looks like this might be what I need to use. This code gives me the error:
Error using cellfun
Input #2 expected to be a cell array,
was double instead.
I no longer have a and b data as this was old code before I used cell arrays, this is now within A and B, so what was 'a' is now contained in A. Would you recommend indexing into A to use this code and potentially remove this error?
I've tried to use the code you've provided with a for loop to go through all the data, and I get the same error as mentioned in the previous comment
angle = (0:3:360)';
for i = (1:length(A))';
d{i} = cellfun(@interp1, A{i},B{i},angle),'UniformOutput',false;
end
The a and b that Guillaume are "dummy variables" for the anonymous functions, not your original a and b. He could have written
newb = cellfun(@(First,Second) interp1(First, Second(:, 2:end)), angle), A, B, 'UniformOutput', false);
If your B is not a cell array then you need to revise your question, as your question is all about the two 5 x 1 arrays being cell arrays.
Guillaume
Guillaume el 7 de Jul. de 2016
Editada: Guillaume el 7 de Jul. de 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!
Jessica Smith
Jessica Smith el 7 de Jul. de 2016
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Preguntada:

el 7 de Jul. de 2016

Comentada:

el 7 de Jul. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by