Storing data from a triple for loop in a matrix

Hello! Can someone help me with the following quation?
I want to make a matrix with outputs of a triple for loop. The code is as follows.
First there is a odefunction that is used in the script.
The the script with the function handle:
for i = 1:3 % The for loops runs 27 times because there are 3^3 options.
for j = 1:3
for k = 1:3
odefunc = @(t,y) odeFunction(t,y,Cint, Cwall, R1(i), R2(j), Rwin(k))
% integrate the system of differential equations from tspan(1) to
% tspan(2) with initial conditions y0
[t,y] = ode45(odefunc, tspan, y0);
end
This computes different outputs with 27 possible input options. I want to store these outputs in matrix of
K = zeros(27, 1440);

 採用された回答

Star Strider
Star Strider 2021 年 10 月 29 日

0 投票

Store the intermediate results in cell arrays, and sort the results out later —
for i = 1:3 % The for loops runs 27 times because there are 3^3 options.
for j = 1:3
for k = 1:3
odefunc = @(t,y) odeFunction(t,y,Cint, Cwall, R1(i), R2(j), Rwin(k))
% integrate the system of differential equations from tspan(1) to
% tspan(2) with initial conditions y0
[t,y] = ode45(odefunc, tspan, y0);
tc{i,j,k} = t;
yc{i,j,k} = y;
end
end
end
If ‘Cwall’ has more than 2 elements, all the ‘t’ vectors and ‘y’ matrices will have the same row dimension, however it is still easier to save (and later address) the results as cell arrays rather than as concatenated matrices.
.

4 件のコメント

Jesper Schreurs
Jesper Schreurs 2021 年 10 月 29 日
Thank you for your reaction. It works fine storing it in cels.
Star Strider
Star Strider 2021 年 10 月 29 日
My pleasure!
Since the intent is only to return the second column in the ‘y’ output, change the ‘yc’ assignment to:
yc{i,j,k} = y(:,2);
since ‘y(2)’ would only store the second value of the first column:
y = randi(9,5,3)
y = 5×3
9 7 2 6 1 1 3 6 7 4 9 1 8 8 9
ye2 = y(2)
ye2 = 6
yv2 = y(:,2)
yv2 = 5×1
7 1 6 9 8
.
Jesper Schreurs
Jesper Schreurs 2021 年 10 月 29 日
Thanks a lot man! You helped me very good.
Star Strider
Star Strider 2021 年 10 月 29 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

その他の回答 (1 件)

Jon
Jon 2021 年 10 月 29 日
編集済み: Jon 2021 年 10 月 29 日

0 投票

It isn't completely clear from your description, but assuming the output you want to save is the vector y from each diff eq solution, and that this always has 1440 elements, you could do this:
K = zeros(27,1440); % preallocate
count = 0;
for i = 1:3
for j = 1:3
for k = 1:3
% increment loop counter
count = count + 1
.
.
.
[t,y] = ode45(odefunc, tspan, y0);
K(count,:) = y(:)'; % use y(:)' to make sure it is a row
end
end
end

1 件のコメント

Jesper Schreurs
Jesper Schreurs 2021 年 10 月 29 日
Thanks for the quick reaction!
The ode function is as follows:
For the output in the matrix K, I only want the y(2) value that the function computes.
function [dydt] = odeFunction(t,y,Cint, Cwall, R2, R1, Rwin)
% set of ordinary differential equations
% input: Cint - constant
% Cwall - constant
% R2 - constant
% R1 - constant
% Rwin - constant
% t - the time variable
% y - the state variable
%output: dydt - the set of equations
% initialize the set of equations
dydt = zeros(2,1);
Tamb = dlmread("Temperaturesummer22.dat"); % Reads the temprature dataset
n = linspace(0,1440,1441) ; % The amount of minutes
Tamb = interp1(n, Tamb,t); % Interpolates the time and temprature data.
% defindlmread("Temperaturesummer22.dat") the set of equations
dydt(1) = ((Tamb- y(1))/(Cwall * R2)) + ((y(2)- y(1))/(Cwall * R1))
dydt(2) = ((y(1) - y(2))/(Cint * R1)) + ((Tamb - y(2))/ (Cint * Rwin))
end
% The script setup:
% The constant inputs which are inputs in the ode function.
Cint = 10000;
Cwall = 500;
R1 = [0.05 0.1 0.4];
R2 = [0.04 0.08 0.3];
Rwin = [0.03 0.1 0.5];
% time dependent window
tspan = 0:1:1440; % calculate from t= 0 and t=1440;
% can define the time interval steps
% i.e. tspan = linspace(0,1440,1441);
y0 = [20;18]; % initial conditions for temprature
K = zeros(27, 1440);
% Here will be the triple for loop.
% Every output y(2) has 1441 rows and there are 27 different outputs.
% I want to have all these outputs stored in a matrix, but I don not know
% how to do it because there is a triple for loop.

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by