How to use Cellfun with both cell type and vector type inputs?

Hi Every one :)
I have a function called FAL which requires 4 vectors as its input
>> function A = FAL (X , Y , Z , W)
...
Now I need to apply this function on different X and Y vectors that are stored in two Cells:
>> Cell_X = { X1 , X2 , X3 , X4 , ... }
>> Cell_Y = { Y1 , Y2 , Y3 , Y4 , ... }
However, Z and W are always constant vectors.
In fact, I need to apply the function each time on one pair of X and Y , while Z and W are not changing.
I am writing my code like this:
>> S = cellfun (@FAL , Cell_X , Cell_Y , Z , W)
So at the end S should be :
S = [ FAL (X1 , Y1, Z , W) , FAL (X2 , Y2, Z , W) , FAL (X3 , Y3, Z , W) , FAL (X4 , Y4, Z , W) , ... ]
But the cellfun is not working, I guess because the type and size of inputs are not identical. What should I do ?
** I can make cells of reapeting vectors with Z and W, but it is not efficient . right ?
*** I am using MATLAB 2015b
Thanks all of you in advance :)

5 Comments

Try
S = cellfun (@(x,y) FAL(x,y,Z,W) , Cell_X , Cell_Y)
Thank you Tommy
I did that, but I am getting this error:
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
I will try to edit my question to make it more precise, and I would really apprecite if you can help me with it
Tommy's strategy is a good one.
S = cellfun (@(x,y) FAL(x,y,Z,W) , Cell_X , Cell_Y, 'uniform', 0)
Perfect
It is working now
Thank you
The only thing is that the result is in a cell form - not a matrix anymore - which is not a big deal. But do you know why ?
FAL is not returning a scalar. You have not shown us the code for it, so we do not know what it computes.

Sign in to comment.

Answers (0)

Categories

Asked:

on 9 Apr 2020

Commented:

on 10 Apr 2020

Community Treasure Hunt

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

Start Hunting!