I currently have code that places data collected from a simulation into various txt documents separated by time. However, I would like to add various headers and other information into each document. How would that be possible? Some of the information would be a variable (such as previous ones I put in) and others would be constant for each one.
for nn = 1:TMAX/10
fid = fopen(['word' num2str(nn) '.txt'],'w');
%x and y are defined here for the entire code and for x y and theta to
%have a close access to print
x = x + vel*cos(theta)*dt;
y = y + vel*sin(theta)*dt;
fprintf(fid, '%4.5f\t%4.5f\t%4.5f\n', x,y,theta);
fclose(fid);
ideally the new code would return
ITEM: TIMESTEP (is the header)
1000 (i a variable base off of time (is nn/10))
ITEM: NUMBER OF ATOMS (another header)
32000 (Variable N)
ITEM: BOX BOUNDS pp pp pp (another header)
0 54 (is variable 0-L)
0 54 (is variable 0-L)
0 6.283185307 (is constant 0 to 2 pi)
ITEM: ATOMS id x y Theta (another header)
the last line that isn't written is that I would like to number each particle. Ie Currently the code returns a matrix of the three variables X Y Theta in three columns and I would like the rows to be labeled 1-N.
How could any of this be done? Thank you very much!
Related
I am trying to write a script that goes through the Buckingham Pi theorem given a list of variables, each having a dimension. The set of dimensions do not need to be unique (it can contain repeats) only the same size as the set of variables.
clc, clear, close all
syms M L T Theta
dimen = [M,L,T,Theta]
mass - M
length - L
time - T
temperature - Theta
(I can add in other dimensions like electric current later, but for now, I want to get it working with these four).
Here is what I have so far in MatLab.
A = L^2; % maybe an area
V = L/T; % maybe a velocity
D = M/L^3; % maybe a density
% This the array of the combinations
param = {A,V,D};
I want to count how many of the syms M L T and Theta show up in my cell param.
For example starting at the first entry in the cell array.
param{1} = A
L^2
At this step, it should count that L has shown up once, and the others 0 times each.
param{2} = V
L/T
At this step, it counts that L has shown up once, but since it was already counted so I don't want to count it again. It should also count that T has shown up once. So far 1 L, and 1 T.
param{3} = D
M/L^3
Finally, this count that M has shown up once. So far 1 L, 1 T, and 1 M.
Since there are four possible symbols, I want to end the algorithm with this.
j = num; % how many times each of the syms was counted at least once.
If a dimension is not counted, that is fine. I am only interested in counting how many times each of the dimensions in the cell array are counted at least once. I will then use these to solve a system of equations to identify dimensionless groups.
I've received some suggestions based on other answers that I am providing below.
How to extract powers of a symbolic polynomial?
present = cellfun(#(expr), ismember(dimen, symvar(expr)), param, 'UniformOutput', false)
counts = sum(vertcat(present{:}), 1)
This last suggestions gives this error.
Error using cellfun
Input #2 expected to be a cell array, was sym instead.
Addendum
Removing the comma as suggested in the comments/answer still gives the same error. I am using release 2021b and a mlx file.
cellfun expects a cell array as its second argument: by introducing a comma between #(expr) and ismember(dimen, symvar(expr)) it's as if you were asking cellfun to iterate over the content of ismember(dimen, symvar(expr)), which is not what you really want, as that's the body of the anonymous function you are passing to cellfun as first argument.
The correct way of using cellfun is shown in the following script:
clc, clear, close all
syms M L T Theta
dimen = [M,L,T,Theta]
A = L^2; % maybe an area
V = L/T; % maybe a velocity
D = M/L^3; % maybe a density
% This the array of the combinations
param = {A,V,D};
% Counts the appearences of each dimension in each param
% and stores them in a cell array of vectors
present = cellfun(#(expr) ismember(dimen, symvar(expr)), param, 'UniformOutput', false)
% Unpack the cell array of vectors and
% compute the total number of appearences of each dimension
counts = sum(vertcat(present{:}), 1)
I have several .mat files, which are all in a 3-D matrix of latitude x longitude x value (70 x 70 x 8760).
It looks like this:
year2000.mat --> (72 x 70 x 8760),
year2001.mat --> (72 x 70 x 8760),until year 2020
I need the long-term mean along the third dimensions-the value. The result should be again a .mat file with 3-Dimension (72 x 70 x 8760). The first and second dimensions don't change.
I am very new to Matlab.
you did well in loading the files but since you know all the files start with year you can do:
fn = dir('year*.mat');
Then, you load the files, but if I understand you correctly you want to mean the years, not each year. so,
data = zeros(72,70,8760);
for n=1:numel(fn)
single_year = load(fn(n).name);
data = data + single_year.variable ;
end
is variable really the name if the variable in each mat file? I just used what you wrote, but you should check what you get if you load a single file.
now the avg is just the sum over the n:
data=data./numel(fn);
Hello I'm new to Matlab.
I've written this script :
k2=2*pi();
z1 = 1;
z2 = 2;
z3 = 4;
for l = linspace(0,1,11)
A = [ -1 1 1 0 ; 1 z1/z2 -z1/z2 0 ; 0 exp(-i*k2*l) exp(i*k2*l) -1 ; 0 exp(- i*k2*l) -exp(i*k2*l) -z2/z3];
B = [ 1 ; 1 ; 0 ; 0];
D = inv(A);
C = mtimes(D,B) ;
display(C)
r = C(1,1); % this is supposed to set r = the 1,1 element in the matrix C
t = C(1,4); % see above
end
My idea for taking the values of r and t from C didnt appear to work. How can I do this properly?
Also I want to plot a graph of |r|,|t|, arg(r) and arg(t) for each value of l, my for loop overwrites the values of r and t? how can I either plot one point per loop or make r and t assign the new values so that they become lists of data.
Thanks a lot!
Matlab sets the first dimension of a matrix as row number (i.e. y position).
So you want t=C(4, 1), as you should see that the size of C is 4x1. As a note Matlab is quite good at suppressing singleton dimensions so you could do also do C(1) and C(4).
For your second point you want to set a particular element of r and t in each loop. This is the same as when you access at particular element of C when setting the values. For your case you can use the index l to determine the element. Remembering that in matlab arrays start at element 1 (not 0 as in many other languages). So you want something like r(l+1)=C(1); (or change l to start at 1).
In the more general case if you are not looping over an integer for some reason you may need to create a separate counter variable which you increase in the loop. Also it is good practice to preallocate such arrays when the size is known beforehand, often by r=zeros(11, 1) or similar (note: zeros(11) is an 11x11 matrix). This isn't significant in this case but can drastically increase execution time for large multi-dimensional arrays so is a good practice.
Is there any way in Matlab to generate a 5000 x 1000 matrix of random numbers in which:
MM = betarnd(A,B,1,1000);
but A and B are vectors (1 x 5000). I get the following error message:
??? Error using ==> betarnd at 29
Size information is inconsistent.
I want to avoid a loop like the following one:
for ii = 1 : 1000
MM(:,ii) = betarnd(A,B);
end
Thanks!
You can repeat A and B (vectors of size 1x5000) to obtain matrices of size 1000x5000 in which all rows are equal, and use those matrices as inputs to betarnd. That way you get a result of size 1000x5000 in which column k contains 1000 random values with parameters A(k) and B(k).
The reason is that, according to the documentation (emphasis mine):
R = betarnd(A,B) returns an array of random numbers chosen from the
beta distribution with parameters A and B. The size of R is the common size of A and B if both are arrays.
So, use
MM = betarnd(repmat(A(:).',1000,1), repmat(B(:).',1000,1));
I have a data file in which I take all of the numbers from 2 columns. The 2 columns of data, which I call 'a' and 'r', have an initial value stored in them which I would like to discount. It also has several runs in the the data, so the initialization is repeated about 15 times. Is there a way that I can graph the log of the points that correspond to a being greater than the initial value (in this case 4) and its corresponding r value? It would be along the lines of
fia = fopen('data.txt');
A = fscanf(fia, '%f %f, [2,inf]);
a=A(1,:);
r=A(2;:);
plot(log(a(4:end)),log(r)); % I know this won't work, as it breaks down the matrix, but something like this.