I have a function with an output q. This function will generate a matrix, A, in order to find q. Matrix A is 100x100x100, so it takes a really long time to run this code and I want to do (lots) of other calculations with the information from A without having to run that code over and over again.
Is there a way to make both q and A (q has dimensions 100x1) accessible in another function without running the original function each time?
I though about using "global" but I'm not sure how that works.
You can use
assignin('base', 'var1', var1)
To assign variable var
in the base work-space. This will allow you to parse it to other functions and it will persist.
Alternatively you can simply return it:
function [other_vars var1] = theFunk(input)
Then to use it in other functions you can make it global:
global VAR_GLOBAL = var;
and in your function use:
function [stuff] = someOtherFunction(input)
global VAR_GLOBAL
% Do some stuff with VAR_GLOBAL
or simply pass it into your other functions:
function [stuff] = someOtherFunction(input, var1)
It appears to me that your function is doing two separate things, and needs some refactoring...
Consider extracting the part that computes the matrix A
into a separate function. The other function will take the produced matrix A
as input and compute the output q
. Otherwise known as the "Extract Till You Drop" principle :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.