I have an column vector with following format in R: num [1:2464, 1].
I want to diagonal the vector, so each element is in the diagonal of the matrix. I have tried the following code:
diagvector <- diag(myvector)
But then it just show the first number. I think I only can use that code if my vector have the following form: num [1:2464].
So how do I a) change the format from num [1:2464, 1] to num [1:2464] for my vector, or b) take the diagonal to my vector with the format num [1:2464, 1]?
Your "column vector" is actually a matrix as it has two dimensions, but it can be formed into a vector.
myvector <- matrix(1:2464, 1)
diagvector <- diag(c(myvector))
diagvector
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] ...
[1,] 1 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0 2 0 0 0 0 0 0 0 0 0 0 0
[3,] 0 0 3 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 4 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 5 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 6 0 0 0 0 0 0 0
[7,] 0 0 0 0 0 0 7 0 0 0 0 0 0
[8,] 0 0 0 0 0 0 0 8 0 0 0 0 0
[9,] 0 0 0 0 0 0 0 0 9 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 10 0 0 0
[11,] 0 0 0 0 0 0 0 0 0 0 11 0 0
[12,] 0 0 0 0 0 0 0 0 0 0 0 12 0
[13,] 0 0 0 0 0 0 0 0 0 0 0 0 13
...
Or:
myvector <- matrix(1:2464, 1)
diagvector <- diag(length(myvector)) * c(myvector)
diagvector
Related
I'm trying to find the 4-neighbors of all 1's in a matrix of 0's and 1's using the J programming language. I have a method worked out, but am trying to find a method that is more compact.
To illustrate, let's say I have the matrix M—
] M=. 4 4$0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 0
and I want to generate—
0 0 1 0
0 1 0 1
0 0 1 0
0 0 0 0
I've sorted something close (which I owe to this little gem: https://www.reddit.com/r/cellular_automata/comments/9kw21u/i_made_a_34byte_implementation_of_conways_game_of/)—
] +/+/(|:i:1*(2 2)$1 0 0 1)&|.M
0 0 1 0
0 1 2 1
0 0 1 0
0 0 0 0
which is fine because I'll be weighting the initial 1's anyway (and the actual numbers aren't really that important for my application anyway). But I feel like this could be more compact and I've just hit a wall. And the compactness of the expression actually is important to my application.
Building on #Eelvex comment solution, if you are willing to make the verb dyadic it becomes pretty simple. The left argument can be the rotation matrix and then the result is composed with +./ which is a logical or and can be weighted however you want.
] M0=. 4 4$0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 0
] m =.2,\5$0,i:1
0 _1
_1 0
0 1
1 0
m +./#:|. M0
0 0 1 0
0 1 0 1
0 0 1 0
0 0 0 0
There is still an issue with the edges (which wrap) around, but that also occurs with your original solution, so I am hoping that you are not concerned with that.
] M1=. 4 4$1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
m +./#:|. M1
0 1 0 1
1 0 0 0
0 0 0 0
1 0 0 0
If you did want to clean that up, you can use the slightly longer m +./#:(|.!.0), which fills the rotation with 0's.
] M2=. 4 4$ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
m +./#:(|.!.0) M2
0 0 0 0
0 0 0 0
0 0 0 1
0 0 1 0
m +./#:(|.!.0) M1
0 1 0 0
1 0 0 0
0 0 0 0
0 0 0 0
I'm trying to convert a compound from mol to adjacency matrix. However, i encountered a problem that rdkit.Chem.rdmolops.GetAdjacencyMatrix() doesn't provide the index of the atoms for the adjacency matrix. Is there any way to include the index data for the adjacency matrix in rdkit?
rdkit.Chem.rdmolops.GetAdjacencyMatrix((Mol)mol)
As the RDKit AdjacencyMatrix is ordered from zero upwards, you can convert it to a Pandas dataframe.
from rdkit import Chem
import pandas as pd
s = 'CCC(C(O)C)CN'
mol = Chem.MolFromSmiles(s)
am = Chem.GetAdjacencyMatrix(mol)
print(am)
[[0 1 0 0 0 0 0 0]
[1 0 1 0 0 0 0 0]
[0 1 0 1 0 0 1 0]
[0 0 1 0 1 1 0 0]
[0 0 0 1 0 0 0 0]
[0 0 0 1 0 0 0 0]
[0 0 1 0 0 0 0 1]
[0 0 0 0 0 0 1 0]]
df = pd.DataFrame(am)
print(df)
0 1 2 3 4 5 6 7
0 0 1 0 0 0 0 0 0
1 1 0 1 0 0 0 0 0
2 0 1 0 1 0 0 1 0
3 0 0 1 0 1 1 0 0
4 0 0 0 1 0 0 0 0
5 0 0 0 1 0 0 0 0
6 0 0 1 0 0 0 0 1
7 0 0 0 0 0 0 1 0
If you want elements instead of indices
element = [atom.GetSymbol() for atom in mol.GetAtoms()]
print(element)
['C', 'C', 'C', 'C', 'O', 'C', 'C', 'N']
df_e = pd.DataFrame(am, index=element, columns=element)
print(df_e)
C C C C O C C N
C 0 1 0 0 0 0 0 0
C 1 0 1 0 0 0 0 0
C 0 1 0 1 0 0 1 0
C 0 0 1 0 1 1 0 0
O 0 0 0 1 0 0 0 0
C 0 0 0 1 0 0 0 0
C 0 0 1 0 0 0 0 1
N 0 0 0 0 0 0 1 0
I wonder what is the most effective way to solve the following problem:
(If there is a name for this problem, I would like to know it as well)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 0 1 0 1 1 1;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
If I have an image where I am interested in the following pixels marked by 1. In an image I want to calculate a sum around this block. A sum of block is easy to calculate from an integral image but I don't want to do it for the whole image, since there is a lot of unnecessary computation.
One option that I can come up with is to search the minimum and maximum in horizontal and vertical directions and then take a rectangular portion of the image enlarged so that it will covered the block portion. For example +2 pixels each directions, if the block size is 5. But this solution still includes unnecessary calculation.
If I had a list of these indices, I could loop through them and calculate the sum for each block but then if there is another pixel close by which has the same pixels in its block, I need to recalculate them and If I save them, I somehow need to look if they are already calculated or not and that takes time as well.
Is there a known solution for this sort of a problem?
I have a datafile contains two 101*101 matrix of float numbers, one is data and the other is error.
It looks like this
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
10381.8 0 0 3462.03 10341 0 6889.64
6919.26 6916.64 3459.49 10349.8 13781.3 6887.57 24157.2
3459.66 0 24158.9 13792.6 3433.65 27579.4 24117.4
0 0 0 0 0 0 0
0 0 0 0 0 0 0
# Errors [Positon_sample/samp_psd.txt] I_err:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
4892.66 4890.8 3459.49 5975.49 6890.64 4870.25 9130.63
3459.66 0 9131.25 6896.32 3433.65 9750.84 9115.54
3464.99 4888.97 5972.77 11419.1 7713.44 8438.29 9093.38
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Now I would like to only plot the first matrix.
I use "plot 'E:\samp_psd.txt' matrix with image"
But the program corrupt...
It seems that I should set the dimension of the matrix,
My case is a little similar like this
Gnuplot plot Matrix over Matrix
I would separate the two matrices with two empty lines like this:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
10381.8 0 0 3462.03 10341 0 6889.64
6919.26 6916.64 3459.49 10349.8 13781.3 6887.57 24157.2
3459.66 0 24158.9 13792.6 3433.65 27579.4 24117.4
0 0 0 0 0 0 0
0 0 0 0 0 0 0
# Errors [Positon_sample/samp_psd.txt] I_err:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
4892.66 4890.8 3459.49 5975.49 6890.64 4870.25 9130.63
3459.66 0 9131.25 6896.32 3433.65 9750.84 9115.54
3464.99 4888.97 5972.77 11419.1 7713.44 8438.29 9093.38
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Then you can access a single matrix with the index command like this:
plot "samp_psd.txt" index 0 matrix with image
how to convert these 7-segment decoder to boolean expression??
BCD 7-Segment decoder
A B C D a b c d e f g
0 0 0 0 0 0 0 0 0 0 1
0 0 0 1 1 0 0 1 1 1 1
0 0 1 0 0 0 1 0 0 1 0
0 0 1 1 0 0 0 0 1 1 0
0 1 0 0 1 0 0 1 1 0 0
0 1 0 1 0 1 0 0 1 0 0
0 1 1 0 0 1 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 1 0 0
I suggest you use a karnough map.
You'll need to use one for each result column, so 7 4x4 tables.
There are even a few karough map generators on the web that you can use.