# Write multiple variables from a function

1 view (last 30 days)
Martyn Steel on 5 Aug 2022
Answered: Star Strider on 5 Aug 2022
I'm aware this is probably a horribly inefficient way of completing my work. What I'm trying to do is run through a workflow with a variety of different permutations of variables and get the large range of outputs for me to analyse.
What I've wrote up so far is:
% Define the CO2 Saturation sampling percentages (every 0.5%)
sat_samp_int=linspace(0,1,200);
% Generate the various saturation logs for the different scenarios
sw_sats=Sw*sat_samp_int;
co2_sats=CO2_Sat*sat_samp_int;
% Loop through the different saturations
for i = 1:200
qq1=sw_sats(:,i)./k_brine;
qq2=co2_sats(:,i)./Kb.';
end
In essence, I want to be able to generate the 200 different scenarios but I think the way I'm writing this so far is that each iteration is overwriting the previous loop so I'm only left with the output with reference to the final column of "sw_sats" and "co2_sats".
Is there a way to implement it so that I can actually be left with all my different iterations visible for me to view? Even if it leaves me with a massive array that is workable for me.
Star Strider on 5 Aug 2022
My pleasure!
I’ll post it as an Answer now, as well.

Star Strider on 5 Aug 2022
sw_sats is a 7071x200 array
co2_sat is 7071x200 array
k_brine is a 1x1500 array
Kb is a 1x1500 array
That means that ‘Sw’ and ‘CO2_Sat’ are (7071 x 1) vectors. Make this straightforward with:
sat_samp_int=linspace(0,1,1500);
and they are now instead (7071 x 1500) matrices. With that, everything has compatible dimensions and straightforward matrix division is possible.
qq1=sw_sats./k_brine;
qq2=co2_sats./Kb;
Make appropriate transpose or reshape operations to get the result you want (since I have no idea what that is).
.

James Tursa on 5 Aug 2022
Edited: James Tursa on 5 Aug 2022
Maybe something like this does what you want, with each qq1 and qq2 2D page results in the first two dimensions.
[m n] = size(sw_sats);
qq1 = reshape(sw_sats,m,1,n) ./ k_brine;
qq2 = reshape(co2_sats,m,1,n) ./ Kb;
That is,
qq1(:,:,1) would be the result of sw_sats(:,1) ./ k_brine
qq1(:,:,2) would be the result of sw_sats(:,2) ./ k_brine
qq2(:,:,1) would be the result of co2_sats(:,1) ./ Kb
qq2(:,:,2) would be the result of co2_sats(:,2) ./ Kb
etc.

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by