regarding matrix comparison in R - r

Currently, I have two matrixes, and want to compare it and see whether they are exactly equivalent. In R, is there any function to do that?

As stated above start with ?all.equal or ?identical. If you then find that your matrices are unequal, you may want to compare them column by column. This might do the trick:
mapply(as.data.frame(m1),as.data.frame(m2),FUN=function(v1,v2) all(v1==v2) )

Related

create a vector increased by 0.5 in julia

Suppose I want to create a vector in Julia. The vector should be [0,0.5,1,1.5,2,....20].
What's the quick command to create a vector like this, from 0 to 20, increasing by 0.5?
This will create a range, which is a kind of vector:
0:0.5:20
This does the same thing
range(0, 20; step=0.5)
If you absolutely need to make a Vector, you can use collect on either of these, but in most cases you should not, and just use 0:0.5:20 directly.
There are many ways to do this. While I prefer the colon array syntax already mentioned, this could be used for more general construction:
[0.5*i for i in 1:20]

generating completely new vector based on other vectors

Assume I have four-vectors (v1,v2,v3,v4), and I want to create a new vector (vec_new) that is not close to any of those four-vectors. I was thinking about interpolation and extrapolation. Do you think they are suitable? Are they also apply for vector and generate a vector of let's say 300 dimensions? Another possible option would be the transformation matrix. But I am not sure if it fit my concern. I think averaging and concatenation are not the good ones as I might be close to some of those four-vectors.
based on my problem, Imagine I divided my vectors into two categories. I need to find a vector which belongs to non-of those categories.
Any other ideas?
Per my comment, I wouldn't expect the creation of synthetic "far away" examples to be useful for realistic goals.
Even things like word antonyms are not maximally cosine-dissimilar from each other, because among the realm of all word-meaning-possibilities, antonyms are quite similar to each other. For example, 'hot' and 'cold' are considered opposites, but are the same kind of word, describing the same temperature-property, and can often be drop-in replacements for each other in the same sentences. So while they may show an interesting contrast in word-vector space, the "direction of difference" isn't going to be through the origin -- as would create maximal cosine-dissimilarity.
And in classification contexts, even a simple 2-category classifier will need actual 'negative' examples. With only positive examples, the 'vector space' won't necessarily model anything about hypothesized-but-not-actually-present negative examples. (It's nearly impossible to divide the space into two categories without training examples showing the real "boundaries".)
Still, there's an easy way to make a vector that is maximally dissimilar to another single vector: negate it. That creates a vector that's in the exact opposite direction from the original, and thus will have a cosine-similarity of -1.0.
If you have a number of vectors against which you want to find a maximally-dissimilar vector, I suspect you can't do much better than negating the average of all the vectors. That is, average the vectors, then negate that average-vector, to find the vector that's pointing exactly-opposite the average.
Good luck!

R reference (documentation, specification)

Is there a specification of R language that covers it?
For example I wanted to know what happens if you multiply a matrix and a vector (possibly transposed), the other way around. Using the standard * operator, not the %*% matrix multiplication. (Is it commutative?) I know I could find the specification of the behaviour for python or c++, but R?
One thing I found:
https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Operators
[...] adding two vectors of the same length will create a vector containing the element-wise sums, implicitly looping over the vector index. This applies also to other operators like -, *, and / as well as to higher dimensional structures. Notice in particular that multiplying two matrices does not produce the usual matrix product.
Then:
[...] (matrix+matrix, dimensions must match. vector+matrix: first recycle, then check if dims fit, error if not)
I'm trying to decode what first recycle, then check if dims fit means.
Should the vector multiply rows of the matrix, or columns? Why should it check if some dims fit when it is recycled?
Like first check if #rows of the matrix is a multiple of the length of the vector? Or #rows times #cols is a multiple of length(vector)?
It does not seem as the specification though as it does not (?) answer such a basic question.

Finding a closest looking segment of data in another sequence

I am doing image processing, in which I came across a situation, where I have to compare two vectors and find an instance of the smaller vector in the larger vector.
Say the two vectors are A: with 100 elements (or entries)
and B; with 10 elements. B is a model and it may not be present exactly as it is' in the vector A. I can compare 10 elements at a time and find the difference. Ideal case is that the B is present somewhere and the difference is zero. Otherwise a minimum will result at some random location, and i am missing the location.
Please help me in giving an algorithm such that the i can find Bs' closest instance in A.
What you are looking for is the cross-correlation function.The peak the the cross correlation of the two vectors will be the point were vector B is most similar to vector A.
You may want to get an explanation of how it is implemented in matlab HERE as it gives an easier explanation of how this operation can be implemented in software.

Position of a variable within a vector

Does anyone know of a simple and efficient way to figure out how many values, in an unsorted vector, are greater than a variable?
My vector is 1,000,000 values long, and I have about 400 of these comparisons to make, with different vectors and variables. Any time-saving function would be appreciated...
If you just want to know how many meet the condition rather than which ones meet the condition, try this:
vector<-c(1,2,3,4,5)
sum(vector>1)
Just use the which function. So if I have vector,
vector<-c(1,2,3,4,5)
which(vector>1)
Outputs 2,3,4,5
if I understand what you want.
you may reorder your vector (quick sort), and after you may a search(binary search). all elements, after the first element that is > then you variable, will be >. is the opposite for <.

Resources