How to run an array on a vector-function?

Hello, I simplified my problem with the following example. I want to calculate 4 solutions of my function by using the values in a,b,c row-wise. e.g. the first solution should be with a=1,b=2,c=4, the second a=4,b=4,c=3, the third a=7;b=6;c=2; the fourth a=9,b=3,c=1. I tried several options like arrayfun and rowfun but was always unsuccessful. I am sorry if the title might be misleading... Thanks for any suggestions
a=[1;4;7;9]
b=[2;4;6;3]
c=[4;3;2;1]
x=(1:1:100)'
function= a*x + b*x + c*x;

1 commentaire

What is the answer you are looking for exactly?
a*x + b*x + c*x
simplifies to
( a + b + c )*x
which is very easy to do, either with bsxfun or implicit expansion, depending on your Matlab version e.g.
x * ( a + b + c )';
in R2018a

Connectez-vous pour commenter.

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 14 Juin 2018
Modifié(e) : Ameer Hamza le 14 Juin 2018
You didn't mention what went wrong with arrayfun. Here is a working example based on the function you gave
a=[1;4;7;9];
b=[2;4;6;3];
c=[4;3;2;1];
x=(1:1:100)';
result = arrayfun(@(a,b,c) a*x + b*x + c*x, a, b, c, 'UniformOutput', false);
since each time, the function returns an array, therefore 'UniformOutput' need to be set to false. It will return a cell array.

Plus de réponses (0)

Catégories

En savoir plus sur Sparse Matrices dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by