What is the ' (single quote) operator in MATLAB? - matlab

I've got some examples on how to use a government-supplied library file, but the examples are written in MatLab. Our project is in C#.
Could someone tell me what this means?
fid = fopen('d:\coilmodel\outlet.txt');
M = fscanf(fid, '%f', [7, inf]);
fclose(fid);
M = M';
I understand I am opening a text file and using that to populate a matrix M that is 7 floating points wide, then it closes the file.
What is M = M';?
I can duplicate all of this in my C# code except for the last line, and my only hurdle is I do not know what the action is doing.
Is this a transform?
Is the matrix being transposed?
I'd like to get a reference to this action so I can do further research.

It is the complex conjugate transpose (or adjoint) of the matrix M, see here.
Note
As Edric specified, ' it's the CTRANSPOSE, i.e. the “adjoint matrix or (complex) conjugate transpose”, which gives the same result when applied on real matrices, but on complex matrices
negates the sign of the imaginary part of the complex elements in A
If you need only to
interchanges the row and column index for each element
then you will use .'.

Note that ' is the CTRANSPOSE operator in MATLAB. If you don't want the complex conjugate, use .' which is the TRANSPOSE method.

Related

Using Matlab writematrix in several scripts to write to the same csv

To analyse some data I have severral scripts that take the same input and calculate different things. I would like to have matlab then write the output of each script to the same csv so I have all the outputs in one place. How do I do this? Is this even possible w/o making a huge matrix in Matlab and writing the whole matrix in one comaand? As far as I can tell writematrix only ever writes to the first column.
Example:
A = rand(1,10)'; B = rand(1,10)';
writematrix(A,'M.xls') %write to column 1 of csv
writematrix(B,'M.xls') %this overwrites the previous command
You can write to different sheets in xcel, but that's not suitable.
The documentation for writematrix is here: https://uk.mathworks.com/help/matlab/ref/writematrix.html
TIA
Specify the range (or the starting cell) for second column using the 'Range' property and use 'append' for WriteMode as shown below:
A = rand(10,1); B = rand(10,1);
%Side-note: ' is complex conjugate transpose.
%Use transpose .' when you want to take transpose
%In this case, you can initialise the random matrices with 10x1 size
%instead of 1x10 size and then taking the tranpose
writematrix(A,'M.xls');
%Assuming that B1 is the starting cell for the second column:
writematrix(B,'M.xls','Range','B1', 'WriteMode', 'append');

What is the colon operator doing here?

What are these lines of code doing?
x0 = rand(n,2)
x0(:,1)=W*x0(:,1)
x0(:,2)=H*x0(:,2)
x0=x0(:)
Is this just one big column vector?
I'd encourage you to take a MATLAB Tutorial as indexing arrays is a fundamental skill. Also see Basic Concepts in MATLAB. Line-by-line descriptions are below to get you started.
What are these lines of code doing?
Let's take this line by line.
1. This line uses rand() to generate an n x 2 matrix of uniform random numbers (~U(0,1)).
x0 = rand(n,2) % Generate nx2 matrix of U(0,1) random numbers
2. Multiply the first column by W
In this case, x0(:,1) means take all rows of x0 (the colon in the first argument) and the 1st column (the 1). Here, the * operator indicates W is a scalar or an appropriately sized array for feasible matrix multiplication (my guess is a scalar). The notation .* can be used for element-by-element multiplication; see here and here for more details.
x0(:,1)=W*x0(:,1) % Multiply (all rows) 1st column by W
3. Multiply the first column by H.
Using similar logic as #2.
x0(:,2)=H*x0(:,2) % Multiply (all rows) 2nd column by H
4. Force column
The x0(:) takes the array x0 and forces all elements into a single column.
From the documentation for colon:
A(:) reshapes all elements of A into a single column vector. This has
no effect if A is already a column vector.
A related operation is forcing a row vector by combining this with the transpose operator.
For example, try the following: x0(:).'
x0 = x0(:) % Force Column
x0 = x0(:).' % Force Row
Related Posts:
What is Matlab's colon operator called?
How does MATLAB's colon operator work?
Combination of colon-operations in MATLAB

Matrix multiplication and differences

I need to compute this equation with MATLAB:
where Sn can be both matrices or scalar and I tried to do it with
S_A = S_3*S_5*((ones-(S_1*S_5)).^(-1))*S_2+S_4
The problem is it doesn't give me the right result and the problem it seems to be with the difference but I cannot figure why is giving me wrong results.
The result is supposed to be this one
but the MATLAB result is
I don't understand why the two results are not the same.
The only way that I figured is through this
diff = ones-(S_1*S_5);
if S_1*S_5 == zeros %Perchè senza non funziona?
diff = ones;
else
diff = (ones-(S_1*S_5)).^(-1)
end
S_A = S_3*S_5*diff*S_2+S_4;
But I don't think it's a smart solution. Anyone knows why I'm not getting the correct results?
"I tried to do it with S_A = S_3*S_5*((ones-(S_1*S_5)).^(-1))*S_2+S_4"
The problem here is that A^(-1) in mathematical notation means "take the inverse", whereas you used A. ^(-1), note the dot, which in MATLAB's notation means "take the each matrix element to the power -1". Taking the inverse of a matrix is not smart in MATLAB anyway, be it via inv() or ^(-1), instead, use mldivide:
S_A = S_3*S_5*(eye(size(S_1*S_5,1))-(S_1*S_5))\S_2+S_4
Also, as mentioned in Brice's answer use eye, not ones to create an identity matrix, and feed it a size argument as opposed to nothing. All in all it looks to me like you do not have a firm grasp of basic MATLAB functionality, so I'd like to point you to The MathWorks own tutorial on MATLAB.
ones outputs a matrix filled with ones, not the identitiy matrix which is given by function eye.
You also need to specify the size of ones or eye, otherwise it will simply output a scalar 1 (i.e. a 1-by-1 matrix filled with ones, or the 1-by-1 identity matrix).
Try (assuming all matrices have the same size):
siz=length(S_1);
S_A = S_3*S_5*((eye(siz)-(S_1*S_5))^(-1))*S_2+S_4

Reshaping of Array in MATLAB

I have a 32768*8 array which I want to convert into a 1*262144 array. I have used the MATLAB command reshape but the problem is reshape changes the matrix row-wise and then appends it to the columns. I have also used the (V:); function but it also does the same as reshape so no use.
I want that the binary data integrity is maintained. So
0F 4B = 0000 1111 0100 1011...etc should be like this and not otherwise as done by the reshape command.
Any ideas?
Thanks!
If you want to reshape it in row major order, just transpose first:
reshape(MyMatrix.', 1, [])
So the .' is the crux of the solution here. (Note that if you're not working with complex numbers then ' and .' do the same thing. I just used .' to be completely correct, but in the vast majority of cases I would just use ')
I would definitely use reshape or : as in previous answers. But, if you have the Communications Toolbox, you can also use vec2mat:
vec2mat(MyMatrix, numel(MyMatrix))
You could also transpose your matrix first then invoke the (:) command too! Make sure you transpose your vector back if you want it to be a row vector.
MyMatrix = MyMatrix.';
MyVector = (MyMatrix(:)).';
Also to be syntactically correct (inspired by Dan - see below), I have also employed .' as using ' will invoke the complex transpose. Not only will it transpose your vector, it will also conjugate your elements as well. This is actually quite useful if you want to calculate the magnitude of a complex vector, as the definition of the magnitude squared is (a + ib)*(a - ib). If I remember correctly, an article by Loren Shure (I can't remember which one) mentioned that the complex transpose was placed in MATLAB for this very purpose.
However, if all of your elements are real, then you can either use ' or .'. Doesn't matter which one.

MATLAB: why transpose single dimension array

I have the following Matlab code snippet that I'm having to translate to VBScript. However, I'm not understanding why the last line is even necessary.
clear i
for i = 1:numb_days
doy(i) = floor(dt_daily(i) - datenum(2012,12,31,0,0,0));
end
doy = doy';
Looking over the rest of the code, this happens in a lot of other places where there are single dimension arrays (?) being transposed in place. I'm a newbie when it comes to both these languages, as well as posting a question on Stack, as I'm a sleuth when it comes to finding answers, just not in this case. Thanks in advance.
All "arrays" in MATLAB have at least two dimensions, and can be treated as having any number of dimensions you wish. The transpose operator here is converting between a row (size [1 N] array) and a column (size [N 1] array). This can be significant when it comes to either concatenating the arrays, or performing other operations.
Conceptually, the dimension vector of a MATLAB array has as many trailing 1s as is required to perform an operation. This means that you can index any MATLAB array with any number of subscripts, providing you don't exceed the bounds, like so:
x = magic(4); % 4-by-4 square matrix
x(2,3,1,1,1) % pick an element
One final note: the ' operator is the complex-conjugate transpose CTRANSPOSE. The .' operator is the ordinary TRANSPOSE operator.