How to multiply variables of a netcdf file with different constants using NCO or CDO? - constants

I have a netCDF file containing different variables (val1, val2, val3). The names of the variables are indicated, in fact, they are completely different from each other.
I want to multiply the variables with different constants such as:
val1*c1
val2*c2
val3*c3
How I could do that with NCO or CDO?

In NCO as decribed here:
ncap2 -s 'val1*=c1;val2*=c2;val3*=c3' in.nc out.nc

In cdo you can use expr in a similar way to nco on each variable.
cdo expr,'val1=val1*c1;val2=val2*c2;val3=val3*c3' in.nc out.nc
see section 2.7.1 of the cdo manual 1.9.9 for more details.

Related

How to create a Matlab symbol composition ∆t?

I'd like to create a single symbol which is contains a delta "∆" and a t.
For visualisation purposes i use Matlab Live Script.
Combining several latin letters is easy done by:
sym('dt')
But these either result in an exception or don't do a conversion:
sym('Delta t')
sym('Deltat')
The workaround to multiply two symbols does not work in all cases:
sym('Delta')*sym('t')
(sym('Delta')*sym('t'))^2
However, if i square the symbol, i'd like to have this behavior:
sym('dt')^2
But it should contain the delta symbol:
Is this what you want?
title('\Deltat')
If you want, you can also do
title('\Deltat^{2}')
The answer is no, you cant use non-ASCII for variables in MATLAB. This means no \Delta, no \epsilon, or any other non-standard chars.
You can generate the LaTeX representation of the results, and then print them either in text in MATLAB or in your favourite LaTeX editor with the function latex

What is the default variable in MATLAB?

What is the default variable in MATLAB if you do not assign one to an equation? I was thinking it was maybe just x?
The answer is ans, this is also shown in the workspace which contains the variables. Consider this example input in the command line:
3*4
ans =
12
Just be sure never to use ans as an actual variable, as that'll mess up your code good.

Writing matlab vector to a file that is matlab readable

For matlab: Is there a way to write the value of a vector to a file that can later be opened and read by another matlab program?
Specifically: I have a matlab program that computes a binary-valued vector $zvector$ with 10^7 entries. I want to write $zvector$ as data to an output file so that it can be emailed and easily read as input to another matlab program. Ideally, the output file would be called “Output.m” and would look like:
zvector=[
0
1
1
…
0
1
];
I like the .m format because it is easy to use for matlab input. I have experimented with matlab’s write() and fwrite() commands, with no success. I observe that these generate files that cannot be easily read as matlab-recognizable inputs (at least, I do not know how to read from them). Is there a way to accomplish my goals? Thanks.
PS: I am interested in the easiest way. If this involves a different type of file format (not a .m format) that is fine. However, in that case, can you provide both the writing and reading commands? Thanks again.
Thanks to #edwinksl for pointing me in the right direction with MAT files. I do not know the accepted practice here, but in stackexchange math it is encouraged to answer your own question if a hint from comments got you all the way there. So I will answer my own question.
The Mat format does this well. Here are example script files for reading and writing in the Mat format (see also links in above comments for more documentation):
***Script file OutputTest.m:
filename = 'TestFile.mat';
TestVector=[1 1 0 1];
save(filename, 'TestVector');
***Script file IntputTest.m
filename = 'TestFile.mat';
file=load(filename);
z =file.TestVector;
z

Why does nothing work in my Matlab plot?

I am trying to plot with Matlab. In particular, I try with numerous online source but none of them work.
Here is my problem, I am trying to plot the expression: y=2*(x-1)/(x-4)Kb/L, and I am interested in the range of x between 0 and 1.
K=40;
b=20;
L=0.5;
x=linspace(0,1,1000);
y=2*(x-1)/(x-4)*K*b/L;
but it returns:
y=275.01
I know linspace isn't the proper way to plot. How can I plot this function? I want to keep the K,b,L declaration because I might change them latter.
y=2*(x-1)./(x-4)*K*b/L; you should use ./ replace /
Like hzy199411 said, you should use the "." operation.
I would suggest that you type "help ." at a MATLAB command prompt. MATLAB will respond with a large index of results but look for the section on "Arithmetic Operators".
You may also try the command "doc arith" but I think the "help ." is more helpful because at least in MATLAB 2013 it verbosely lists more "dot" operators.
In short several arithmetic operators prefixed with '.' ("Dot") are "Element-by-Element" operations and as such they operate on each index of the array/matrix.
For example if you had an array s=1:20 and you performed the operation s/s you would get ans = 1, where as if you did s./s you would get an array of 1's with the same length as 's'.
I guess that you are a new matlab user :). The program is in general ok, but you should think of some things. First,
linspace is not a plotting function. The function is useful though. With your syntax it creates a vector of length 1000 with range [0,1]. For plotting, type:
plot(x,y);
Linecolor and style can be set as
plot(x,y,'r-.');
For predefined colors (here 'r-.' means a red dotted line). There are also some additional properties that can be found be checking the online help of plot.
Also as the others say, if you want to operate on each element in the vector, use ./. The / is a matrix operator.

What is the # operator (at sign) in MATLAB?

I have some MATLAB programs that use the # (at sign) as an operator. What does it mean?
Does MATLAB 6.5 support this operator?
The # operator creates a function handle, something that allows you to easily create and pass around a function call like a variable. It has many nice features, none of which are available to you unfortunately. This is because as you suspect, it was not introduced into matlab until version 7, the release immediately after yours.
It used to declare Anonymous Functions in Matlab.
I think the terms is "Function Handle".
Practically it covers the inability of Matlab to declare a function at any place in any M file.
You may see it here:
What is your favourite MATLAB/Octave programming trick?
I found it to be useful in Image Processing along with the "blockproc" command.
Documentation says that it's a function handle.