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.
Related
I am working on matlab with a matrix. I would like to reproduce this matrix and apply sum for elements in rows.
I have two vectors defined by this code:
unitsvector=1:5;
reordervector=1:3;
Then, I create an empty matrix to store the values:
resultvec=zeros(size(unitsvector,2)*size(reordervector,2),3);
Finally, here is the loop I use but it is not working:
for a=1:length(resultvec)
for b=reordervector
for c=unitsvector
resultvec(a,1)=b;
resultvec(a,2)=c;
resultvec(a,3)=b+c;
end
end
end
How could I reproduce this matrix in matlab. Thanks for your help.
You can use meshgrid for this without a for loop.
[a,b] = meshgrid(1:5,1:3);
M = [a(:) b(:)];
M(:,3) = sum(M,2); % Create third column by summing first two
Why are you looping at all? sum actually has vector support; a simple resultvec = [a(:,1),a(:,2),sum(a,2)] would work.
As to your code: of course it doesn't work. What do you expect to be the contents of a? You create a as a loop index, which runs over the range 1:length(resultvec). Ergo, within each loop iteration a is a scalar. You try to call it like it is a three-element vector. Nor do you define b and c. This might be possible in R, judging where you're coming from, but not in MATLAB.
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.
I'm trying to combine all these 2d matrices I have in .dat files into a single 3d matrix.
So what I've done so far is this:
for (i=1:40) //There are 40 of these files
fileName = ['Solutionm1.dat/Solution' num2str(i-1) '.dat'] //This line sets a file name
f=fopen(fileName,'r') //I'm just opening the file now
A{i}=fread(f,'double') //Now I'm converting the binary file into a matlab matrix
B{i}=reshape(A{i},41,21) //Now I'm putting it into the shape that I want (I don't know of a better way to read binary files)
fclose all
end
Ultimately, I want to take the L2 norm of this 3d matrix by using norm((insert 3d matrix here),2)
The problem I'm having is that I just don't know how to combine all the matrices I just made into one big 3D matrix.
Solution
Use
T(:,:,i)=B{i}
or use
T=cat(3,B{:})
Continued problem
This doesn't work now:
norm(T,2) //Should compute the L2 norm of my 3D matrix, right?
This might be out of the scope of this question though...
From what I've learned, I think norm has to be used on a 2D matrix.
Here's the answer!
T=Cat(3,B{:}) //Combines all matrices into one single 3D matrix
Once you have B, run:
matrix3d = zeros(41,21,40);
for i=1:40
matrix3d(:, :, i)=B{i};
end
You can also include matrix3d(:, :, i)=B{i}; in your loop, and call matrix3d = zeros(41,21,40); before entering the loop.
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.
A question about matlab and randomisation of a 3d matrix respecting the rows and columns.
I have a n x n x s matrix M and I want to mess it up a bit, but with some control.
I can achieve my wish with a for loop
for j=1:size(M,3)
r=randperm(size(M,1));
random_M(:,:,j)=M(r,r,j);
end
Is there a way to perform this without having to loop over j? I need many randomisation iterations and could afford the benefits of indexing.
Cheers!
edit: Some more thoughts following Alexandrew's comments
I have created a function that randomises a squeezed version of M:
function randomMat=randomiseMat(Mat)
[rows,cols]=size(Mat);
r=randperm(rows);
randomMat=Mat(r,r);
then, using arrayfun I seem to get what I want:
randomM=arrayfun(#(x) randomiseMat(M(:,:,x)),1:size(M,3),'UniformOutput', false)
however, randomM is now a cell array of size (1,size(M,3)) with each cell containing randomised array.
Is there a way to make it in a 3d matrix just like the input M?
You can calculate all the values for r in one go, and then use arrayfun:
[nRows,nCols,nPages] = size(M);
[~,r]=sort(rand(nRows,nPages));
%# you should test on a realistic example whether a for-loop
%# isn't faster here
outCell = arrayfun(#(x) M(r(:,x),r(:,x),x), 1:nPages,'UniformOutput',false);
randomM = cat(3,outCell{:});