Reputation: 2805
In the documentation i have found a function findfirst
which is capable of returning the index of the first element, which is equal to the given one.
In my case, i have a vector (or a one dimensional array) and i want to find the first column, which is equal to the vector.
I know how to do it the "hard" way: With findnext
iterating over the first row, checking then the whole column. But is there a smarter way, which isn't obvious to me?
Upvotes: 1
Views: 333
Reputation: 2805
This is the way i go now. It does not feel very smart. I am somehow still confident that there is a better way, however i don't see it right now.
Coming from C/C++ and Python my look my look somewhat weird. I have no idea about good taste in julia. Suggestions are welcome.
function findfirstcolumn(A, v)
index = findfirst(A[1,:],v[1])
found = false
while index != 0 && found == false
found = true
for i = 2:size(v)[1]
if A[i,index] != v[i]
found = false
break
end
end
if found == true
return index
end
index = findnext(A[1,:], v[1], index+1)
end
return 0
end
Upvotes: 0
Reputation: 18217
Suppose m
is your matrix, and v
is the vector.
Then:
findfirst(c->view(m,:,c)==v,1:size(m,2))
Should return 0
if the vector is not found and the column number if it is. Going down to basic element accesses might be faster, but this should also do the trick.
Upvotes: 5