I want to build a loop in Matlab where 'i' is the iteration variable and for each 'i' an n*n matrix will be the outcome.
my question is how can I tell the program to save those n*n matrices each one individually where each matrix has the name of Ai for example.
thanks a lot.
Related
I can not solve a very simple problem in the Simulink: summation of 2 equal size vectors and writing the result into the Matlab workspace.
The trivial operation that takes 1 line in Matlab seems a real problem in the simulink.
I have 2 vectors with the same size, for e.g. 10x1 and I want to get their summation result into the workspace with the same size (10x1).
I have already used 'sum' block for that and even my own function with element-wise summation, but I think the problem is that Simulink block 'to workspace' always concatenate outputs either along 1-st or 3-rd dimension. Hence the size of the output does not inherit the size of inputs.
I can not find any solution in the web, will be really appreciate for your help!
I didn't notice the vectors are saved in a column-based using "to workspace" block. Did you try to add "(:)" in your code to get it in a single column?
As I know, storing the data in columns (1x10) is faster than in rows (10x1). Maybe that is the reason for getting columns instead of rows.
https://www.mathworks.com/matlabcentral/answers/216512-which-is-faster-a-row-vector-or-a-column-vector-can-anyone-answer-me-please
i have a matrix c[38,39], and want to merge it to a new one lets say f[1,1482]. So first i pre-allocate the f and then in a for loop i tried to merge it, but it keeps saying that i exceeded the matrix dimensions.I know that there is the cat function but i get the same results, maybe the reshape function will help? Any advice appretiated, thanks in advance.
f=[]; %// pre-allocating the mew matrix
for k=1:1482 %// 1482=38*39
f(:,k)=[c(:,1);c(:,k)]; %// merging
end
It exceeds matrix dimensions because the second dimension of c is 39 and your loop is referencing values up to 1482.
Reshape may help if you are looking at different matrix sizes but if you are just wanting to convert from a matrix to a vector then just using the (:) notation works.
c=rand(38,39);
f=c(:)';
ps. pre-allocating f only really helps if you specify the final matrix size. Here all you have done is declared an empty matrix which then expands on each loop iteration.
I have a loop which creates me 11 special oxo matrices.
for i=1:1:11
%create the special matrices called D_i
D_i
end
And now I am trying to save this matrices to reuse them in another program
A = cat(1,NaN(o),D_1,D_2,D_3,D_4,D_5,D_6,D_7,D_8,D_9,D_10,NaN(o),D_11,NaN(o),NaN(o))
How can I do that?
Thank you for your help.
So I have 10 matrix, they are 20x20 size. Here, I want to store the first row of the first matrix and store onto the variable call f_row1, then the first of of the second matrix to f_row2......
So A1, A2..... A10 is my 20x20 matrix.
f_row1=A1(:,1);
f_row2=A2(:,1); %and so on.....
Is it possible for me to do it in a loop? How could I do this process in a loop?
As #beaker said, there may not be a good solution. You can use eval to achieve what you want as follows:
for i=1:10
eval(['f_row' num2str(i) '=A' num2str(i) '(:,1);']);
end
I haven't tested it but should work.
Last beginner question of the day...Working in matlab. Suppose one has matrices A1,...,An. How can one call do something to the Ai-th matrix on the i-th iteration of a loop? Actually I would like to concatenate all the matrices except the Ai-th one, but I can probably figure that out.
My solution is to do the following:
Instead of creating matrices in the way I did above, I will create an array of matrices.
for i=1:n
A{i}= matrix with nrows:ncolumns;
end
Then you call the matrix in the obvious way.