Build a matrix of data and insert them in a function

6 views (last 30 days)
I have a function. Suppose
function y=A(1)*B+A(2)
in which A is an array of 2 x m elements and B is a vector of n elements. The A(1,:) is varies while A(2,:) is a contant number. I would like to multiply all the elements of B with each one of the elements of A in order to build a matrix of y outputs.
My ultimate goal to mesh all this data and produce surface plot. I would like to use for loops to avoid the dots before the operators because actually I have a complicated function Thank you.

Accepted Answer

Giorgos Papakonstantinou
Giorgos Papakonstantinou on 21 Dec 2012
What I wanted was the following:
for i=2:4
B(i)=B(1,i-1);
for j=2:11
A(1,j)=A(1,j-1);
A(2,j)=A(2,j-1):
y(i,j)=myfun(A(:,j),B(i));
end
end
figure
mesh(x(1,:)B,y)
surf(x(1,:)B,y)
But I did it. Thank you

More Answers (2)

Walter Roberson
Walter Roberson on 19 Dec 2012
It was not clear how A(2,:) fit in, but perhaps
bsxfun(@times, B(:), A(1,:)) + A(2*ones(1,length(B)), :)

Giorgos Papakonstantinou
Giorgos Papakonstantinou on 19 Dec 2012
Indeed I have to be more clear. I understand the use of bsxfun. But what if I have a much more complicated function?
And to give you more details. If the vectors would be
A(1)=[1 2 3 4 5 6 7 8 9 10];
A(2)=[0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1];
B=[5 8 10];
and the function would be
y=A(1)*A(2)+(A(1)+0.2)^2+B*A(1);
situation is a bit more complicated. So I would like to have the outcome of such a function for all combinations. For instance the outcome of y for B(1) for all A(1,:) and A(2,:), the outcome of y for B(2) for all A(1,:) and A(2,:) ans so on.

Community Treasure Hunt

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

Start Hunting!