How to find indices of all empty rows in a sparse matrix, - julia

I have a large sparse matrix M. I would like to find the indices of all the empty rows in the matrix. How can you do that?

Julia uses the compressed sparse column (CSC) format for sparse matrix storage, which means that the row index for all stored values are available. You can thus find all rows which have no stored value by taking the set difference between 1:NROWS and the set of row indices:
julia> using SparseArrays
julia> A = rand(10, 10); A[3,:] .= 0; A[5,:] .= 0; S = sparse(A);
julia> idx = setdiff(Set(1:size(A, 1)), Set(S.rowval))
Set{Int64} with 2 elements:
3
5

Related

Julia: broadcasting `findfirst()` across rows of a matrix

I want to find the index of the first value in each row of a matrix that satisfies some condition. I want to figure out how to do this without using array comprehensions.
This is how I would do it with an array comprehension:
# let's say we want to find the first column index, per row, where a number in that row is below some threshold.
threshold = 0.5;
data = randn(50,100);
first_threshold_crossings = [findfirst(data[i,:]<threshold) for i in 1:size(data,1)];
Yielding a list of indices that tells you where (column-wise) each row has a value that first drops below the threshold, going from left to right.
Any faster way you can imagine doing this?
Here's how you can do it:
julia> using Random # For RNG reproducability
julia> A = rand(Random.MersenneTwister(0), 3, 3)
3×3 Array{Float64,2}:
0.823648 0.177329 0.0423017
0.910357 0.27888 0.0682693
0.164566 0.203477 0.361828
julia> findfirst.(x < 0.1, eachrow(A))
3-element Array{Union{Nothing, Int64},1}:
3
3
nothing
Note that findfirst returns nothing if no index satisfies the condition.

Matrix multiplication inside for loop in 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);

R - Get Coordinate or List of Lists representation of a sparse martix

I have a sparseMatrix in R as implemented in the Matrix package. I'd like to perform some operations on the column numbers of non-zero entries. Based on how it's stored and printed it seems there should be an easy function to do this but I can't find one in the docs. As a toy example where f(Matrix, Int) is the function I'd like,
M <- Matrix(c(1,0,0,1,0,1,0,0), nrow = 2)
# 1 0 0 1
# 0 1 0 0
f(M,1) = [1,4]
f(M,2) = [2]
So given a matrix and row number we get returned a list of indices of non zero values.
Note: I don't want to iterate over all columns of the data set (there are many millions of them), I'd like some way to get at how the sparseMatrix is stored in memory or an inbuilt efficient conversion to a List of Lists or Coordinate List forms.
You can possibly try:
with(summary(M), j[i == 1])
# [1] 1 4
with(summary(M), j[i == 2])
# [1] 2
Wrap it as a function, assuming M is a sparseMatrix already:
f <- function(M, row) with(summary(M), j[i==row])
f(M, 1)
# [1] 1 4
f(M, 2)
# [1] 2
Did you try str(M) ? it will show matrix actually keeps data.
By default it stored in column-major format - dgCMatrix. But you can convert it to triplet (coordinate list) and row-major format with as() function.
M_triplet = as(M, 'TsparseMatrix')
M_row = as(M, 'RsparseMatrix')
Then you can perform all your operations elementwise and convert it back to dgCMatrix (which is native for almost all Matrix methods).

PARI/GP: How to get the max prime factor of the integer?

I am new to pari/gp. I use factorint to find all the prime factors and it returns a matrix.
I am trying to traverse through a matrix to find the largest number inside but unable to find the length of rows and columns. Also how can i use the if to compare each element is higher or lower. My p is being generated on top.
temp = factorint(p-1);
num = 0;
for(i=1, size,
for(j=1, size,
if(num <= temp[i,j], num = temp[i,j]);
);
);
print("number is = " num);
Thanks in advance.
Please, note that factorint(p) always returns the nx2-matrix, where n is the number of the prime factors for p. First column is for the prime factors. Second column is for their multiplicities.
So all you need is to find the maximum element of the first column. It can be done as follows:
factors = factorint(p-1);
print("number is = ", vecmax(factors[, 1]));
By the way, the length of vector v is just #v in PARI/GP.
Besides matsize, you could also use #. For example,
factorint(30) gives a 3x2 matrix;
[2 1]
[3 1]
[5 1]
#factorint(30) gives the number of colums of that matrix (which is 2).
By transposing the matrix, the resulting matrix has 3 columns, which is the number of distinct prime factors of 30.
Transposing can be done as follows: append ~ to a matrix.
So we could do
#factorint(30)~
to get the number of distinct prime factors;
which prints
[2 3 5]
[1 1 1]
As those prime factors will be increasingly ordered in the first row, the last one in the first row is the largest one hence
factorint(30)[#factorint(30)~, 1] gives the largest prime factor of 30; 5
Now you could avoid factoring 30 twice by doing;
f = factorint(30); f[#f~]
to get 5 as desired.

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