 # Index of an element in a vector after a lot of operation.

Hi everyone,
I need to know how to individuate the index of an element after a lot of operetion.
For example, having this vectors:

A, vector 200x1
B, vector 50x1
k, vector 30x1 i 'll take the element of k one by one. k[i] is the i-th element!
Q, vector 200x1

and i need to know the index of the element

`element=min(abs(abs(abs(k[i]*A[-which(B==B)])-Q[-which(B==B)])-mean(Q)))`

i need to know the index of A that give me element!

i tried to use "which" but i don't know how to indicate the element. The only way to do that that i found is:

``````vector_of element=abs(abs(abs(k[i]*A)-Q)-mean(Q))[-which(B==B)] # vector 1x200 - elements of B (1x50) result  vector 1x150
element=min(vector_of_element)
index_element=which(vector_of_element==element)
``````

do you know a way to do that from the previous string?

thank you so much!

I think you should share some of your data so we know what you're trying to do. The vectors are different lengths, so they will be recycled - you might get a warning about this. Do you intend this?

which(B==B) is meaningless, B is always == B, so the result will be seq(length(B))
A[-which(B==B)] will therefore remove the first length(B) elements from A
I don't think this is what you are trying to do?

apart from this, you could put the result in temp and then find the position of the min of temp (there could be more than one)

``````temp <- abs(abs(abs(k[i]*A[-which(B==B)])-Q[-which(B==B)])-mean(Q))
element <- which(temp == min(temp))``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.