The question is:
How can I change an element in a matrix in Maple?
Is it possible?
Yes, it is certainly possible. (It's one of the primary properties of a Matrix, it is a mutable data structure. ie. its entries can be changed.)
If you have assigned a Matrix to the name A, then you can change the entry in the 4th row and 5th column by using normal assignment:
A[4,5] := 17.34;
For more on manipulating Matrices, Vectors, and Arrays, see the rtable_indexing Help page
Note that Matrix is different from matrix, the latter of which is now deprecated.
Related
I have to two evenly sized very large vectors (columns) A and B. I would like to divide vector A by vector B. This will give me a large matrix AxB filled with zeros, except the last column. This column contains the values I'm interested in. When I simple divide the vectors in a Matlab script, I run out of memory. Probably because the matrix AxB becomes very large. Probably I can prevent this from happening by repeating the following:
calculating the first row of matrix AxB
filter the last value and put it into another vector C.
delete the used row of matrix AxB
redo step 1-4 for all rows in vector A
How can I make a loop which does this?
You're question doesn't make it clear what you are trying to do, although it sounds like you want to do an element wise division.
Try:
C = A./B
"Matrix product AxB" and "dividing vectors" are distinct operations.
If we understood this correctly, what you do want to calculate is "C = last column from AxB", such that:
lastcolsel=zeros(size(B,2),1)
C=(A*B)*lastcolsel
If that code breaks your memory limit, recall that matrix product is associative (MxN)xP = Mx(NxP). Simplifying your example, we get:
lastcolsel=zeros(size(B,2),1)
simplifier=B*lastcolsel
C=A*simplifier
MATLAB documentation of SVD states that the diagonal matrix returned has singular values in decreasing order. Is there a way to find out what the natural ordering of singular values would be?
The reason I ask is because the singular values correspond to dimensions associated with rows of the input matrix.
No, the very definition of SVD does not introduce an ordering. Restricting the discussion to square X matrices and adopting the same notation of the cited matlab documentation, if X = U*S*V' is a SVD of X, then for every permutation matrix P, we can form a valid SVD as X = (U*P)*(P'*S*P)*(V*P)'. Presenting matrix S with descending values is just a matter of convenience: every permutation P'*S*P would do the same job.
As a side note: P*X = P*U*S*V' showing that a row permutation of matrix X does not change the singular values S, which can be considered independent from any row (or column) permutation of X.
I was hoping to get some idea of what is being asked here before responding. For example, the eigenshuffle tool I've posted on the file exchange allows you to reorder the eigenvalues and eigenvectors of a sequence of eigen-problems, so they are maximally consistent with each other in sequence. Perhaps your problem is similar, thus you might think of the singular values as functions that vary along with some parameter that drives a system.
But really, there is no natural ordering of the singular values that comes from the method used to compute the SVD. In fact, the only ordering that makes sense is the one that comes out - decreasing order. The order of the singular values is not dependent on the sequence of the rows of your matrix, as the question seems to vaguely imply, so I'm not sure what is meant there.
Feel free to modify the question in case you can make your needs clearer.
preface: As the matlab guiderules state, Usually, when one wants to efficiently populate a sparse matrix in matlab, he should create a vector of indexes into the matrix and a vector of values he wants to assign, and then concentrate all the assignments into one atomic operation, so as to allow matlab to "prepare" the matrix in advance and optimize the assignment speed. A simple example:
A=sparse([]);
inds=some_index_generating_method();
vals=some_value_generating_method();
A(inds)=vals;
My question: what can I do in the case where inds contain overlapping indexes, i.e inds=[4 17 8 17 9] where 17 repeats twice.
In this case, what I would want to happen is that the matrix would be assigned the addition of all the values that are mapped to the same index, i.e for the previous example
A(17)=vals(2)+vals(4) %as inds(2)==inds(4)
Is there any straightforward and, most importantly, fast way to achieve this? I have no way of generating the indexes and values in a "smarter" way.
This might help:
S = sparse(i,j,s,m,n,nzmax) uses vectors i, j, and s to generate an m-by-n sparse matrix such that S(i(k),j(k)) = s(k), with space allocated for nzmax nonzeros. Vectors i, j, and s are all the same length. Any elements of s that are zero are ignored, along with the corresponding values of i and j. Any elements of s that have duplicate values of i and j are added together.
See more at MATLAB documentation for sparse function
Consider having a matrix. From this matrix I select the same number of elements from every row. Let us say that the matrix is nxn and from each row I take m elements (m<n).
I will build a mxm matrix with this selected elements. In every row I put the elements taken from the original matrix (same row index of course).
What is the best way to achieve this?
Thankyou
One way to achieve this is illustrated here. Define an array a to play around with ...
a = randi(6,6);
b = a([1 3 5],[2 4 6])
This demonstrates the use of index vectors for selecting rows and columns from one matrix into another. It depends on being able to specify the vectors you want to use as indices. You could also write:
c = a(1:2:end,2:2:end)
Now, if you tell us what you mean by 'the best way' we may be able to tell you that too !
EDIT
So I read the question again, it seems by 'best' you mean 'fastest'. I've never been concerned to measure the speed of this sort of operation, I await with interest one of the real Matlab experts who lurk hereabouts providing a much cleverer answer than this.
Of course, the fastest way is to not build a submatrix at all, but to operate on the elements of the original matrix. Whether your algorithm can be adapted to avoid building a submatrix is unknown to me.
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