Combining matrices by using for loop

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.

 Accepted Answer

Stephen23
Stephen23 on 21 Feb 2018
Edited: Stephen23 on 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 Comments

Thank you so much for answering my question, I checked it in Matlab. the code works properly. But my algorithm is a result of solving ode15s as:
[t,y]=ode15s(@(F,initials)second_order1(F,initials),tspan,initials);
For this reason I replaced (nan) in { arr=nan(11,8,n) } by (y) in which y is a (11,8) matrix for n times.
As a result I got:
Index exceeds matrix dimensions.
Error in solutionsecond_order1 (line 17)
arr = y(11,8,n );
So could you please recommend me to fix this error. Kind regards
I used nan(...) to preallocate the output matrix. You should not need to change this preallocation. Once the array is preallocated then call your ODE solver in the loop and add its output matrix to the preallocated array, just as I showed you in my answer.
The results from the ODE are added inside the loop (not before the loop), for example like this:
arr = nan(11,8,n);
for k = 1:n
[t,y] = ode15s(...);
arr(:,:,k) = ... new matrix
end
Thank you again, I followed your procedure, but when I am plotting the resulted matrix, it looks different. I attached to you my codes please could you help me... be notified that solutionsecondorder1.m is the main Matlab code, the other codes are subfiles that I used them inside it, you can save mymodel1 as xlsx then it could be run.
"but when I am plotting the resulted matrix, it looks different"
I don't know what you expect to see, or what you are comparing it against. If you are unsure what the code in my answer does, here is a simplified example of it using four 2x3 matrices:
>> A = nan(2,3,4);
>> A(:,:,1) = [1,2,3;4,5,6]; % add matrix 1
>> A(:,:,2) = [9,8,7;6,5,4]; % add matrix 2
>> A(:,:,3) = [0,0,0;1,1,1]; % add matrix 3
>> A(:,:,4) = [2,2,2;3,3,3]; % add matrix 4
>> reshape(permute(A,[1,3,2]),[],3)
ans =
1 2 3
4 5 6
9 8 7
6 5 4
0 0 0
1 1 1
2 2 2
3 3 3
You can see that my code has allocated the four matrices as four pages of array A, and then the last line has reshaped that 3D array A into a 2D matrix. Note that the input matrices appear in the output sequentially (the rows appear in the order of the matrices), as this was my interpretation of what you requested in your question: "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". If my interpretation of your question is not correct then please let me know how this output array should be arranged.
Mr Stephen, your interpretation is correct, I agree with you 100%. I checked on simple examples everything is O.K. But as I told you, it was not working with my resulted matrix. in my code I am using ode15s 96 times for each time it gives me (11,8)matrix, while I am using your interpretation, the resulted matrix is (11,8,96) with nan in all elements my result is located only in the final part.
@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.
Thank you for everything. I am a beginner in Matlab, I wrote these codes for plotting dynamics of a system which consist of nonlinear differential equations, but the plot that is obtained with your help differs from the response plotted by the author. I know that you helped me so much, but If possible could you edit all my codes for obtaining the required result. If you agree with me, please let me know I will attach to you the equations. kind regards

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!