To store Output of a user defined function in an array

Suppose I have a user defined function which has only one input "b" and one output argument "e", e.g.
function e=user_defined(b)
global u
u=[1 2 3 4];
x1=b(1)+b(2)^2+b(3)+b(4);
x2=u(1)-u(2)^2+u(3)+u(4);
e=(abs(x1-x2)).^2;
%End of the function.
Now if I call this function from a metaheuristic algorithm, say for example Flowe Pollination algorithm and I want to save the values of e , i.e. the o/p of user_defined function in an array A and also I want to pass all the values of e to the Flower Pollination algorithm and store them there in another array B. Then I want to arrange the two arrays A and B in descending order and display them. How can i do that?

回答 (1 件)

KSSV
KSSV 2020 年 9 月 19 日
編集済み: KSSV 2022 年 1 月 3 日

0 投票

If you want to call function n times and if function gives one output, to store the output in array:
B = zeros(n,1) ;
for i= 1:n
B(i) = myfunc(inputs) ;
end

3 件のコメント

Eleanor Stevens
Eleanor Stevens 2022 年 1 月 2 日
Hi!
I'm confused about one line of code...
" zeros(n, 1) "
What does this mean? How does it work?
Thanks :)
Image Analyst
Image Analyst 2022 年 1 月 3 日
It makes a column vector of zeros -- an n row-by-1 column array of all zeros. It preallocates space for the variable to make it a little faster when assigning values in the loop. It doesn't matter how it works. Just assume it gives you a column vector of zeros and don't worry about its internals.
Eleanor Stevens
Eleanor Stevens 2022 年 1 月 3 日
Ah that makes sense. Thank you!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2020 年 9 月 19 日

編集済み:

2022 年 1 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by