reshape function should change the shape of a matrix.
But if I try using it (it's also written in the manual) I discover that if I declare:
reshape(A,m,n);
Then A must have m lines and n columns.
If I try using reshape passing as arguments numbers different from these, I get an error.
So appearently, it does not reshape any matrix, it just does return the same matrix if I pass m and n as arguments, and return an error otherwise.
So if I have a 4x4 matrix and I want to make it smaller: 2x2, I can't.
It changes the shape, not the size of the array. To change the shape the number of elements must not change. So if you have 4x4 you can go to 8x2 or 2x8 or 16x1 etc. but not 2x2 (what do you expect to happen to the other elements?)
Reshape rearranges elements; if you do B = reshape(A, u) then logically B must have the same number of elements (note that size(B) == prod(u)) as A (length(B(:)) == length(A(:))). Otherwise, how would it know which elements to drop if A had more, or where would it get new ones if B had more?
One situation in which reshape is useful is when for some reason your square matrix was unrolled into a vector (perhaps by another function) and you simply need to arrange it back to its previous form.
When you want to get a smaller part of a matrix, use A(i1:i2, j1:j2). When you want to "tile" a matrix, use repmat(A, i, j).
As #thrope says, reshape changes the shape, not the number of elements. If you have a 4x4 matrix and you want the upper left 2x2 corner of it, use B=A(1:2,1:2) or the bottom right 2x2 corner, B=A(3:4,3:4).
Related
so I'm trying to study a code written in MATLAB. And there are these two strange lines of code, which I can't seem to understand, maybe someone could help me out? I'm new to MATLAB, I'm coding in C# most of the time.
As far as I know diag(A) means that it takes the members of main diagonal of matrix A. But what about the other parts of the line? Especially the 1./ operation, what does it do?
In the code below
A is a 4x4 matrix, which stores double type values, b is the coefficients vector and alpha is a freely chosen vector (10, 5, 4, 2).
Atld=diag(1./diag(A))*A-diag(alpha)
btld=diag(1./diag(A))*b
diag(A) returns a vector with the diagonal elements of matrix A
./ is the element-wise division operator, so 1./diag(A) inverts the elements from this vector.
diag(1./diag(A)) returns the diagonal matrix from that vector
So, basically, diag(1./diag(A)) is a matrix with the inverse of the diagonal of A on its diagonal, and zeros everywhere else.
I have a square matrix of size C by C and I want to build a diagonal block matrix with it repeated N=2C(1+C) times. The problem is that the value of C can change, and so I don't know if I could use blkdiag, as I don't know the number of parameters I should enter because the size of the matrix is a variable that the user chooses. How could I do this in MATLAB?
you can use
M = kron(eye(N),A);
where A is the CxC matrix that repeats N times....
Because this will scale rather quickly , for sparse implementation use:
M = kron(speye(N),A);
MATLAB question:
I have an array A(2,2,2) that is three-dimensional. I would like to define a 2x2 array as a subarray of A, as follows:
B = A(1,:,:).
That is, we are simply projecting on the first component. But matlab will now treat this 2x2 matrix as a 1x2x2 array instead, so that I can't do certain things (like multiply by another 2x2 matrix).
How do I get B as a 2x2 subarray of A?
If you think about a skyscraper, your A(1,:,:) is taking the first floor out and this operation inevitably happens across the 3rd dimension.
You can use reshape(), squeeze() or permute() to get rid of the singleton dimension:
reshape(A(1,:,:),2,2)
squeeze(A(1,:,:))
permute(A(1,:,:),[2,3,1])
squeeze() pretty much does all the job by itself, however it is not an inbuilt function and in fact uses reshape(). The other two alternatives are expected to be faster.
You'd want to use the function squeeze which removes the singleton dimensions:
B = squeeze(A(1,:,:))
I have an image, which I have listed as a matrix. I want to take the transpose of that image and then display that image on the screen.
I would like to know how to do this the "hard" way, ie without using MATLAB's transpose function.
function [B] = trans(A)
[r c] = size(A);
B = zeros(c,r);
for i = 1:r
for j = 1:c
B(j,i) = A(i,j)
end
end
end
As this is for a class, I won't give you an exact answer, but I will nudge you in the right direction. I'm assuming that you are looking for a method that involves manually transposing the information, rather than using builtin functions.
Matlab stores values in a matrix in the form of a vector and a "size" - for instance, a 2x3 matrix would be stored with six values in a vector, and then [2,3] (internally) to tell it that it's 2x3 and not 6x1.
For the 2x3 matrix, this is the order of the values in the vector:
1 3 5
2 4 6
To reference the value in (2,2), you can reference it as A(2,2), or as A(4). The value in (1,3) can be referenced as A(5).
As such, if you can construct a vector referencing the values in the transposed order, then you can assign the new values into the appropriate order and store them in a matrix of appropriate size. To make the point, consider the transpose of the above matrix:
1 2
3 4
5 6
This would be represented as (1,3,5,2,4,6) with size (3,2). If you can construct the vector (1,3,5,2,4,6), then you can use that vector to assign the values appropriately.
Here's hint toward a solution: The transpose transforms the element A(1,2) from the normal array A into the element B(2,1) from the transposed array B (B=A'), and vice versa. Thus, you can iterate through all the rows and column, and apply the transformation element-by-element.
Are you allowed to use flipud and fliplr ?
In this case you can represent the transposition as a sequence of flips (I'll leave it to you to figure out the exact sequence).
You can use rot90 with flipud/fliplr for this.
Is there a way in Octave to compute and store only the diagonal of a matrix product?
Basically like doing: vector = diag(A*B);
I don't care about any of the values of A*B except those on the diagonal. The matrix sizes are around 80k x 12 and 12 x 80k, so even if I didn't care about the speed/extra memory it simply wont fit in RAM.
Strange, since Octave is a package for huge data sets and diagonals are very important, so it should be possible.
The first element in the diagonal is the scalar product of the first row of A with the first column of B. The second element in the diagonal is the scalar product of the second row of A with the second column of B.
In other words:
vector = sum(A.*B',2);
This is how you could do it in MATLAB (probably similar to Octave syntax):
vector = sum(A.*B',2);
This will compute only the resulting diagonal of the operation A*B as a column vector vector.
actually I think it's the dot product of the first row of A with the first column of B... the second diagonal element is the dot product of the second row and the second column... etc