MATLAB read a desired number of matrices from a cell array - arrays

I have the following cell array
C_Array <5000x1>
And inside C_Array I have
<ix2 single> where i = a changing integer
I only want to look at the first 2000 matrices in my cell array.
My attempt has been
for j = 1:2000
My_Desired_Range = C_Array{j,:}
end
But this just gives me the matrix for {2000,1} so how do I produce a cell array with just the matrices ranging from {1,1} to {2000,1} ?

Quite simply as mentioned in the comments by #Wauzl
My_Desired_Range = C_Array(1:2000,:)

Related

Multidimensional array : Split and put back together

I am trying to turn a 3 dimensional array "upside-down" as:
I have tried the inverse function, but if we look at the inverse operation in mathematical terms, it gives us another result. I need to turn without changing the data in the array. How to do that?
To split the 3-dimensional array (A x B x C) into A-sub-array (2d, B x C) I have used squeeze: k=squeeze(array(n,:,:)). Now I have a 2-dimensional array of size B x C. How to put it back together (in 3 dimensional array)?
You can use permute() to change the order of dimensions, which can be used as a multidimensional transpose.
Putting 2D matrices into a 3D one then is a simple indexing operation. Read more in indexing here.
A = rand(10,10,10);
B = permute(A, [ 3 2 1 ]); % Permute he order of dimensions
mat1 = rand(10,10);
mat2 = rand(10,10);
mat_both(:,:,2) = mat2; % Stack 2D matrices along the third dimension
mat_both(:,:,1) = mat1;
mat_both = cat(3,mat1, mat2); % Stacks along the third dimension in a faster way

How to store array in a matrix in multiple run time

I was trying to make a matlab code to store arrays in it. I want to create a matrix of 5 rows and 3900 columns to store five different arrays in it.
Each time I run my matlab code it should store the array in the first empty row and next time I run the code the matrix should store the new array in the second empty row and so on until all five rows are occupied by five different arrays.
here is my code :
matrix = zeros(5,3900);
k=1;
d=5;
if k<=d
matrix(k,:) = Array;
k=k+1;
end
this code does not give my what I want
even this one does not do the correct job as I described above
matrix = zeros(5,3900);
k=1;
if (k<=5)
matrix(k,:) = Array;
k=k+1;
end
and this
matrix = zeros(5,3900);
for k=1:5
matrix(k,:) = Array;
end
What should I do to store one array of size 1*3900 in only one row in the first time and in the next run I want that the another array stores in the second row and so on until row number 5?
thank you all
From my understanding you want to run the code several times, but each time the array is different, which is filling up the matrix.
In your example you're overwriting matrix and k each time you run the code and that's (if I understand you right) is preventing from correct execution. What you could do is something like:
initialize before you run the first time your code: matrix = zeros(5,3900);and k=1;
Run afterwards your first code without matrix = zeros(5,3900);
k=1;
In this case matrix and k should increment as you wish it.
Edit:
The first code, which you posted, will do the following (each time you run it):
create an matrix of size [3,3900] with all 0 -entries. if the variable matrix already exists, it will be completely overwritten
same for k and d but with 1 and 5, respectively
check if k<=d: this condition is always true because k = 1 and d = 5
assign on the first row of matrix the values of array(first row because k = 1)
add 1 to k
So you see the statement #5 has no influence because each time you run your code k and matrix are overwritten.
So what you have to do is to NOT set matrix = zeros(5,3900);
k=1; at the begin of your code. In this way matrix and k remain the same. But you have to take care to initialize at k and matrix before you run the first time your code (if not Matlab doesn't know what k is)
Is this clearer now?
Micha

Matlab: initialize empty array of matrices

I need to create an empty array of matrices, and after fill it with matrices of the same size.
I have made a little script to explain:
result = [];
for i = 0: 4;
M = i * ones(5,5); % create matrice
result = [result,M]; % this would have to append M to results
end
Here result is a matrix of size 5*25 and I need an array of matrices 5*5*4.
I have been researched but I only found this line: result = [result(1),M];
The issue is that [] implicitly concatenates values horizontally (the second dimension). In your case, you want to concatenate them along the third dimension so you could use cat.
result = cat(3, result, M);
But a better way to do it would be to actually pre-allocate your result array using zeros
result = zeros(5, 5, 4);
And then within your loop fill each "slice" of the 3D array with the values.
for k = 0:4
M = k * ones(5,5);
result(:,:,k+1) = M;
end

Matlab: creating 3D arrays as a function of 2D arrays

I want to create 3d arrays that are functions of 2d arrays and apply matrix operations on each of the 2D arrays. Right now I am using for loop to create a series of 2d arrays, as in the code below:
for i=1:50
F = [1 0 0; 0 i/10 0; 0 0 1];
B=F*F';
end
Is there a way to do this without the for loop? I tried things such as:
F(2,2) = 0:0.1:5;
and:
f=1:0.1:5;
F=[1 0 0; 0 f 0; 0 0 1];
to create them without the loop, but both give errors of dimension inconsistency.
I also want to perform matrix operations on F in my code, such as
B=F*F';
and want to plot certain components of F as a function of something else. Is it possible to completely eliminate the for loop in such a case?
If I understand what you want correctly, you want 50 2D matrices stacked into a 3D matrix where the middle entry varies from 1/10 to 50/10 = 5 in steps of 1/10. You almost have it correct. What you would need to do is first create a 3D matrix stack, then assign a 3D vector to the middle entry.
Something like this would do:
N = 50;
F = repmat(eye(3,3), [1 1 N]);
F(2,2,:) = (1:N)/10; %// This is 1/10 to 5 in steps of 1/10... or 0.1:0.1:5
First pre-allocate a matrix F that is the identity matrix for all slices, then replace the middle row and middle column of each slice with i/10 for i = 1, 2, ..., 50.
Therefore, to get the ith slice, simply do:
out = F(:,:,i);
Minor Note
I noticed that what you want to do in the end is a matrix multiplication of the 3D matrices. That operation is not defined in MATLAB nor anywhere in a linear algebra context. If you want to multiply each 2D slice independently, you'd be better off using a for loop. Doing this vectorized with native operations isn't supported in this context.
To do it in a loop, you'd do something like this for each slice:
B = zeros(size(F));
for ii = 1 : size(B,3)
B(:,:,ii) = F(:,:,ii)*F(:,:,ii).';
end
... however, examining the properties of your matrix, the only thing that varies is the middle entry. If you perform a matrix multiplication, all of the entries per slice are going to be the same... except for the middle, where the entry is simply itself squared. It doesn't matter if you multiple one slice by the transpose of the other. The transpose of the identity is still the identity.
If your matrices are going to be like this, you can just perform an element-wise multiplication with itself:
B = F.*F;
This will not work if F is anything else but what you have above.
Creating the matrix would be easy:
N = 50;
S = cell(1,N);
S(:) = {eye(3,3)};
F = cat(3, S{:});
F(2,2,:) = (1:N)/10;
Another (faster) way would be:
N = 50;
F = zeros(3,3,N);
F(1,1,:) = 1;
F(2,2,:) = (1:N)/10;
F(3,3,:) = 1;
You then can get the 3rd matrix (for example) by:
F(:,:,3)

how to make matlab loop over 2d array faster

I have the above loop running on the above variables:
A is a 2d array of size mxn.
mask is a 1d logical array of size 1xn
results is a 1d array of size 1xn
B is a vector of the form mx1
C is a mxm matrix, m is the same as the above.
Edit: expanded foo(x) into the function.
here is the code:
temp = (B.'*C*B);
for k = 1:n
x = A(:,k);
if(mask(k) == 1)
result(k) = (B.'*C*x)^2 / (temp*(x.'*C*x)); %returns scalar
end
end
take note, I am already successfully using the above code as a parfor loop instead of for. I was hoping you would be able to suggest some way to use meshgrid or the sort to yield better performance improvement. I don't think I have RAM problems so a solution can also be expensive memory wise.
Many thanks.
try this:
result=(B.'*C*A).^2./diag(temp*(A.'*C*A))'.*mask;
This vectorization via matrix multiplication will also make sure that result is a 1xn vector. In the code you provided there can be a case where the last elements in mask are zeros, in this case your code will truncate result to a smaller length, whereas, in the answer it'll keep these elements zero.
If your foo admits matrix input, you could do:
result = zeros(1,n); % preallocate result with zeros
mask = logical(mask); % make mask logical type
result(mask) = foo(A(mask),:); % compute foo for all selected columns

Resources