Matrix multiplication inside for loop in Scilab - scilab

I want to multiply each column of matrix A by matrix C. For this I am using for loop as follows:
A=[ 0. 1. 2. 3;0. 1. 2. 3.]
C=[2 0;0 2].
for i=1:4
B(i)=C*A(:,i);
end
But no matrix B(i) is displaying.

The result of C*A(:,i) is a column matrix. To store all columns in a single matrix, you have to use the same notation you used to retrieve a single column from A. Therefore, you should write this in your loop:
B(:,i) = C * A(:,i);

Related

BLAS routine to compute diagonal elements only of a matrix product?

Say I have two matrices A and B. I want to compute the diagonal elements of the matrix product A * B and place them in a pre-allocated vector result.
Is there a BLAS (or similar) routine to do this as fast as possible?
There is no specific routine for that. However, you can use the following definition of matrix multiplication.
Consider C = AB, and aij, bij, cij to denote the (i,j)th element of the corresponding matrices. Without loss of generality, I will assume that all A,B,C are N x N dense matrices.
Then,
cij = sumk=0N-1 (aik, bkj)
Since you are interested only in the diagonal entries:
cii = sumk=0N-1 (aik, bki), for i=1,...,N
In other words, to calculate the ith diagonal matrix of matrix C you need to find a dot product between the ith row of matrix A and ith column of matrix B. That can be achieved by using a dot product BLAS level-1 function ?dot.
res = ?dot(n, x, incx, y, incy)
Let's assume that matrices A and B are stored column-wise and are accessible via pointers *A and *B (which hold N*N values), while *C is a preallocated storage for diagonal entries of matrix C (which holds N values).
The following loop should give you the diagonal:
for (int i=0;i<N;i++)
{
C[i] = ?dot(N,A[i],N,B[i*N],1);
}
Notice, that we are accessing the ith row of matrix A by passing the first element of the ith row: A[i], and using increment (incx) of N. In contrast, to access the ith column of matrix B we pass the first element of the ith column: B[i*N] and use increment of 1.
Notes:
if A,B, and C have different (but consistent with matrix multiplication) dimensions, only slight modifications will have to be applied.
if matrices are stored row-wise, the call to ?dot should be slightly changed
the pseudocode above uses a general ?dot function. In practice, it will be sdot or ddot for single- or double precision real numbers, and versions of ?dotu: cdotu and zdotu for complex numbers of single and double precision, respectively.
is it the most efficient, cache-friendly, etc-etc implementation? probably not, but it would surprise me if that becomes a bottleneck in an algorithm where NxN matrices A and B have been explicitly calculated anyway.

transforming a matrix in R by

I want to make a new matrix B from a previous matrix A, where the length of rows and columns are the same in B and every position corresponds to a ranking of A.
In particular, for any x of a location [i,j] in A, I want to find how many values are greater than [i,j] (which sum(A>x), which I can find when x is discrete, but not for any x), followed by division by the total number of observations*variables in the matrix A.
I think using the apply function would be able to create matrix B as I wish, but I'm having trouble finding a way to apply use of "sum" for each position (i.e., sum(A>x)/# of positions in A.
I think I could use apply(A, c(1,2), FUN(X...)), but I do not know what function I can use.
Thanks for any suggestions.
Short version: matrix((length(M) - rank(M))/length(M), nrow=nrow(M), ncol=ncol(M))
Long version:
length(M) will give you the number of elements in the matrix.
length(M) - rank(M) will give the number of elements greater than each element.
So you want (length(M) - rank(M)) / length(M) but formatted into a matrix like M, so
matrix((length(M) - rank(M))/length(M), nrow=nrow(M), ncol=ncol(M))

Creating Vector in R (multiple conditions)

Need to create and print a vector in R that includes the following in this order:
A sequence of integers from 6 to 10 (inclusive)
A twofold repetition of the vector c(2, -5.1, -33)
The value of the sum of 7/42 and 2
a) Then extract the first and last elements of the vector to form another vector
b) Form a third vector from the elements not extracted above
* Use the vectors from (a) and (b) to reconstruct and print the original first vector
That should do it:
a.vec<-c(seq(6,10,1),rep(c(2,-5.1,-33),times=2),(7/42+2))
b.vec<-a.vec[c(1,length(a.vec))]
c.vec<-a.vec[-c(1,length(a.vec))]
a.vec<-c(b.vec[1],c.vec,b.vec[2])

creating many matrix from one matrix with looping?

I have a square matrix M with 25x25 dimension.
Then I want to create 25 matrices as follow:
the first matrix is matrix M without the first row and first column,
the second matrix is matrix M without the second row and second column, - ... so on until 25th matrix.
this little snippet will do:
lapply(1:25, function(i) M[-i, -i])

Zeroing one or more matrix rows or columns

I have a full matrix of numbers. On a computer, I can easily set with zeroes a row or a column. I would like to know how I can represent this operation symbolically in a mathematical expression.
For a n x n matrix A and with
e = ones(n)
e[k] = 0
matrix multiplication
A*diag(e)
zeros the k-th column and
diag(e)*A
zeros the k-th row

Resources