Good to know this method. Thanks a lot.

在 2016年7月6日星期三 UTC+9上午1:45:21,Cedric St-Jean写道:
>
> In Julia, if speed isn't too important, this gives the same results:
>
> a, b = [-1,-2,-3], [-3,-4,-5,-2]
> inter = intersect(a, b)
> (Int[findfirst(a, x) for x in inter], Int[findfirst(b, x) for x in inter])
>
> And it should be a good deal faster than the MATLABism. Other functions 
> you might find useful: ind2sub, div (integer division)
>
> On Tue, Jul 5, 2016 at 12:20 PM, siyu song <[email protected] 
> <javascript:>> wrote:
>
>> Thanks, Fred, for your answer. But in fact I want to know the index of 
>> the common elements of two integer vectors(Elements are all different in 
>> each vectors).
>> For example, v1 = [1,2,3] and v2[3,4,5,2]. So the answer should be 
>> common_index1 = [2,3], common_index2 = [1,4].
>> I use a function as 
>> function find_common(a,b)
>>    a = reshape(a,length(a),1);
>>    b = reshape(b,1,length(b));
>>    la = length(a);
>>    lb = length(b);
>>    a = a[:,ones(1,lb)];
>>    b = b[ones(la,1),:];
>>    comab = find(x->x==true,a .== b);
>>    comab = comab.';
>>    coma = mod(comab+la-1,la)+1;
>>    comb = floor(Int64,(comab+la-1)/la);
>>    return coma,comb;
>> end
>>
>> So coma and comb is exactly what I want. In matlab this is easy to do. 
>> But with julia, I haven't thought of a clever answer yet.
>> In matlab we can simply get coma and comb by [coma, comb] = find(a==b).
>>
>> 在 2016年7月5日星期二 UTC+9下午7:02:34,Fred写道:
>>
>>> julia> a=[1,3,5,7]
>>> 4-element Array{Int64,1}:
>>>  1
>>>  3
>>>  5
>>>  7
>>>
>>>
>>> julia> b=[2,3,5,6,7]
>>> 5-element Array{Int64,1}:
>>>  2
>>>  3
>>>  5
>>>  6
>>>  7
>>>
>>>
>>> julia> intersect(a,b)
>>> 3-element Array{Int64,1}:
>>>  3
>>>  5
>>>  7
>>>
>>>
>>> julia> union(a,b)
>>> 6-element Array{Int64,1}:
>>>  1
>>>  3
>>>  5
>>>  7
>>>  2
>>>  6
>>>
>>>
>>>
>>> Le lundi 4 juillet 2016 04:18:10 UTC+2, siyu song a écrit :
>>>>
>>>> But intersect doesn't tell us the index of the elements in the 
>>>> matrix(array), I think. 
>>>>
>>>
>

Reply via email to