How to load vectors as input parameters and calculate results for each element in a multiscript program?

3 次查看(过去 30 天)
Hi all, I have created a multiscript program with global variables that calculates a lot of things. So far, I have 3 input parameters set as structures, one value each. So far, I load the desired numbers and do the calculations.
x = 0.25, y = 182.2, z = 45.1
Now, I want to substitute x,y,z with 3 vectors like
x = [0.25 0.50 0.75 100], y = [182.2 175.1 172.7 177.6], z = [45.1 35.8 35.7 31.7]
Then I want to run the program (many scripts) and calculate results for every element of these 3 input vectors. Namely, if I had 1 output before, now I want to have one vector with 4 elements. It should not be hard to do but I am kinda new to matlab so your help would be valuable!

回答(1 个)

Walter Roberson
Walter Roberson 2015-9-17
Convert at least one of your scripts into functions that take three parameters, x, y, and z. The function does not need to change to handle multiple values of x, y, and z, but it does need to return the scalar result of the calculation. For example, if before you had RunMyCode.m that expected x, y, and z to be scalar values, and it calculates MyResult, then you would change the file to
function MyResult = RunMyCode(x, y, z)
Once you have done that, have other code that creates x, y, and z as vectors. Then in that code,
OverallResults = arrayfun(@(X,Y,Z) RunMyCode(X,Y,Z), x, y, z);

类别

Help CenterFile Exchange 中查找有关 Financial Data Analytics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by