Reshaping of Array in MATLAB - 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.

Related

how to compute all the minors with a given order of a matrix in matlab

I have a matrix m*n,
I want from it all the minors (the determinant of the submatrices) of order p.
I din't found anything good in the documentation, I could do it with a function written by my self, but I'd prefer something out of the box.
My real need is to check when,in a symbolic matrix, I have a fall of rank,and that happens when all the minors of that rank and above are zeros.
Any idea to do it with pure matlab comands? since there is a function to evalutate rank it has get the minors someway.
There appear to be some good answers already, but here is a simple explanation of what you can do:
Suppose you want to know the rank of every i-jth submatrix of a matrix M.
Now i believe the simplest way to get all ranks is to loop over all rows and columns and store this result in a matrix R.
M = magic(5);
R = NaN(size(M));
for i=1:size(M,1);
for j=1:size(M,2);
R(i,j) = rank(M([1:i-1 i+1:end],[1:j-1 j+1:end]));
end
end
If you want all determinants replace rank with det.
This calculates the submatrix:
submatrix=#(M,r,c)M([1:r-1,r+1:end],[1:c-1,c+1:end])
You may either use 'arrayfun' and 'meshgrid' or two loops to iterate over all submatrices.
Caveat: I don't have the Symbolic Toolbox but for a regular matlab array you can calculate the i-th, j-th minor with an anonymous function like this:
minor = #(i,j,A)det(A(setdiff([1:end],[i]),setdiff([1:end],[j])))
Or if you want the i-th, j-th cofactor, simply use:
cofactor = #(i,j,A)(-1)^(i+j)*det(A(setdiff([1:end],[i]),setdiff([1:end],[j])))
But as mentioned I don't know if something like this will work with the Symbolic Toolbox. If it does not work as-is, perhaps this can at least give you some ideas on how you might implement the function for the symbolic case.
Hope this helps.

automatic transpose of vectors for binary operations

I know there are alternatives exist. But just curious to know. When I perform some binary operations such as *,-,/,+ between two vectors of same size, some times the dimension does not match. For eg., for a*b a is of size (m,1) and b is also of size (m,1). or for a-b, the size of a,b is (m,1) and (1,m) respectively. Is there a way that matlab automatically matches dimension of vectors and performs the operation.
A simple approach is to use
a(:)-b(:)
instead of a-b. The linear indexing (:) turns everything into a column vector.
If one of the operands is in turn the result of an operation, for example b+c, you can't directly write a(:)-(b+c)(:) in Matlab. In that case you can use reshape, like this:
reshape(a,[],1) - reshape(b+c,[],1)
This works because reshape(...,[],1), like (:), converts its argument into a column; but now that argument can be the result of an operation.

Cepstrum deconvolution Matlab code

I have a little code, that should implement cepstrum deconvolution for minimum phase FIR filter design, but being nonmatlab guy I'm struggling with understanding it. Can someone help?
wn = [ones(1,m)
2*ones((n+odd)/2-1,m)
ones(1-rem(n,2),m)
zeros((n+od d)/2-1,m)];
y = real(ifft(exp(fft(wn.*real(ifft(log(abs(fft(x)))))))));
Mainly I don't understand the first line, ".*" symbol in second line and also probably at some point there should be conversion from real to complex domain in the second line, but I have no idea where. Any ideas?
In the first line you are constructing the matrix wn row by row.
.* operator means element-wise multiplication. * alone would mean matrix multiplication.
In fact you should pay attention to the size of x and wn which must be the same for the element-wise multiplication to have sense.
actually there isn't any conversion from real to complex in the second line. There are the functions log, fft, ifft that may return complex values depending on the input.
You can access the Matlab help by the commands help or doc (for example doc ones should produce the documentation of the ones function - this produce a matrix filled with ones of the size specified by it's arguments).
To quickly summon the help when you're inspecting some code you can use Matlab's inline help by pressing the button F1 when the cursor is at the end of a function name (just before the parenthesis).

Matlab dwt across specified dimension

I have a dataset Sig of size 65536 x 192 in Matlab. If I want to take the one-dimensional fft along the second dimension, I could either do a for loop:
%pre-allocate ect..
for i=1:65536
F(i,:) = fft(Sig(i,:));
end
or I could specify the dimension and do it without the for loop:
F = fft(Sig,[],2);
which is about 20 times faster for my dataset.
I have looked for something similar for the discrete wavelet transform (dwt), but been unable to find it. So I was wondering if anyone knows a way to do dwt across a specified dimension in Matlab? Or do I have to use for loops?
In your loop FFT example, it seems you operate on lines. Matlab use a Column-major order. It may explain the difference of performance. Is the performance the same if you operate on columns ?
If this is the right explanation, you could use dwt in a loop.
A solution if you really need performance is to do your own MEX calling a C discrete wavelet transform library the way you want.
I presume you're using the function from the Wavelet Toolbox: http://www.mathworks.co.uk/help/toolbox/wavelet/ref/dwt.html
The documentation doesn't seem to describe acting on an array, so it's probably not supported. If it does allow you to input an array, then it will operate on the first non-singleton dimension or it will ignore the shape and treat it as a vector.

What is the ' (single quote) operator in 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.