Combining matrices by using for loop
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Shirin Muhammad
am 21 Feb. 2018
Kommentiert: Shirin Muhammad
am 23 Feb. 2018
Every iteration my algorithm gives me a matrix with eleven rows and eight columns, I want to form a new matrix composed of these matrixes together sequentially, as a result, a newly formed matrix of (11*n)rows with 8 columns must be obtained. where n is the number of iterations. I will be thankful if anyone suggest me how to write a loop which can do so.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 21 Feb. 2018
Bearbeitet: Stephen23
am 23 Feb. 2018
n = number of iterations
arr = nan(11,8,n);
for k = 1:n
new = ...
arr(:,:,k) = new
end
and then to get the data into one long matrix:
mat = reshape(permute(arr,[1,3,2]),[],8)
7 Kommentare
Stephen23
am 23 Feb. 2018
Bearbeitet: Stephen23
am 23 Feb. 2018
@Shirin Muhammad: your scripts need to be tidied up:
- in your script solutionsecond_order1.m every iteration of the k loop (the inner loop: see below) calculates exactly the same thing. What is the point of that?
- You define y1, ttot, E, and GG without using them afterwards.
- You do not need to define an anonymous function, just ode15s(@second_order1, tspan, initials) will do perfectly.
- The basic problem is that your code is very badly aligned. Badly aligned code is one way that beginners hide mistakes in their code. When I align your code properly (ctrl+i in the editor) it looks like this:
clear all;
y1 = zeros(11, 8); %ss=zeros(1056,768);
ttot = 0;
initials = [0; 0; 0; 0; 0; 0; 0; 0];
FF = xlsread('mymodel1.xlsx');
syms uc(t) wr1(t) wr2(t) p1(t) p2(t)
DAEvars = [uc(t); wr1(t); wr2(t); p1(t); p2(t)];
E = zeros(11, 8);
GG = zeros(11, 1);
for i = 1:96
tspan = [0 FF(i, 1)];
F = FF(i, 4); % !!! last line with i !!!
%combining y data
n = 96; %number of iterations
arr = nan(11, 8, n);
for k = 1:n
[t, y] = ode15s(@(F, initials)second_order1(F, initials), tspan, initials);
arr(:, :, k) = y; %new matrix
end
%and then to get the data into one long matrix:
mat = reshape(permute(arr, [1, 3, 2]), [], 8);
time = linspace(0, 5, 1056);
for j = 1:5
subplot(3, 2, j)
plot(time, mat(:, j), '-o')
%plot(t,y(:,j))
hold on
S{j} = char(DAEvars(j));
legend(S(j), 'Location', 'Best')
end
end
I highlighted the last line where you use the loop iterator i. After this any matrices or plots are simply overwritten (because you do not use i). What you have done is to loop over 1 to 96 twice using nested loops (outer loop for i = 1:96, inner loop for k = 1:n). Because you reallocate all relevant variables within the outer loop only the last iteration of the outer loop values are stored, thus throwing away any value that you might have calculated on previous loop iterations.
What you want is something like this:
...
n = 96; %number of iterations
arr = nan(11, 8, n);
for k = 1:n
tspan = [0,FF(k,1)];
F = FF(k,4);
[t, y] = ode15s(@(F, initials)second_order1(F, initials), tspan, initials);
arr(:, :, k) = y;
end
mat = reshape(permute(arr, [1,3,2]), [], 8);
... plot
Do NOT use another nested loop inside or around that loop and you will find that all of your data is in arr. My answer did not use nested loops, nor do any of my comments... and you don't need them either.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!