Reputation: 27
I have the following script to calculate fluid perturbations. I am reading in 2D velocity fields and then storing this data in a cell array. In the present example i have only 4 velocity fields from different time steps, but eventually I will have around 300+
The velocity fields are stored in the cell array and that part works. What i need help with is to create a loop to then reshape the matrices in the cell array and store as a separate matrix.
So far i have
%% Calculating the Perturbation
% The perturbation is calculated by subtracting the average velocity form
% the instantaneous.
% Below are the instantaneous velocity fields in each direction
% u velocity
U = num2cell(u,1);
% v velocity
V = num2cell(v,1);
% w velocity
W = num2cell(w,1);
From here i now want to reshape the matrix in the cell arrays and save as follows:
%% Reshape the velocity vectors into matrices
u1d = reshape(cell2mat(U(1,1)),[nx ny]);
u2d = reshape(cell2mat(U(1,2)),[nx ny]);
u3d = reshape(cell2mat(U(1,3)),[nx ny]);
u4d = reshape(cell2mat(U(1,4)),[nx ny]);
v1d = reshape(cell2mat(V(1,1)),[nx ny]);
v2d = reshape(cell2mat(V(1,2)),[nx ny]);
v3d = reshape(cell2mat(V(1,3)),[nx ny]);
v4d = reshape(cell2mat(V(1,4)),[nx ny]);
w1d = reshape(cell2mat(W(1,1)),[nx ny]);
w2d = reshape(cell2mat(W(1,2)),[nx ny]);
w3d = reshape(cell2mat(W(1,3)),[nx ny]);
w4d = reshape(cell2mat(W(1,4)),[nx ny]);
now this method is very long and i am not sure how to create a loop to reshape the matrix and then rename them as shown above. Individually will take too long and i want to process 300+ files. I would definitely appreciate help with this. I have done it manually as thats were my knowledge goes to.
The reason i want it as u1d, u2d...v1d, v2d .... w1d...
is to calculate the two point correlation. So once i get u1d....
etc i will subtract it from the average velocity field. Which is another part of the code. I need help to create a loop for this section.
Upvotes: 1
Views: 292
Reputation: 60504
First, some pointers:
cell2mat(U(1,1))
is the same as U{1,1}
. You should prefer the latter, it's much more efficient because it doesn't do a function call.
Naming variables u1d
, u2d
, u3d
, ... is usually a bad idea. You already figured you could use a cell array U
to store your vectors, you should store these also in a cell array: ud{1}
, ud{2}
, ud{3}
, ...
Given these two points, you can do your thing using a loop:
for ii=1:4
ud{ii} = reshape(U{ii},[nx ny]);
end
However, using a cell array in this case is actually not even necessary, it is just as much work to do indexing like this: U{1,1}
as like this: u(:,1)
. You should prefer the latter, as that is how your data comes in: you avoid the copy needed to generate the cell array.
Given you numeric array u
of size n
xk
, with k
vectors (k=4
in your example), and n==nx*ny
the number of elements in each, you can do:
ud = reshape(u, nx, ny, k);
which is the same as
ud = reshape(u, nx, ny, []);
In this last version MATLAB figures out what k
should be given the size of u
.
From here on, you index ud(:,:,1)
to get your u1d
, etc.
Do note that reshape
does not copy data. ud
will reference the same data as u
until you change one of the two arrays. As soon as you try to change data in one of them, a copy of the full array will be made. This is called "lazy copying". If you don't need to preserve u
, I recommend that you don't create a new variable, simply do u = reshape(u...)
. This will prevent the data from being copied.
Upvotes: 2