Re: [julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-21 Thread Joshua Ballanco
Use `enumerate`:

julia> a = [1,3,5,8]
julia> b = [1,2,5,7]
julia> intersect(enumerate(a), enumerate(b))
2-element Array{Tuple{Int64,Int64},1}:
 (1,1)
 (3,5)


On July 5, 2016 at 13:20:15, siyu song (siyuphs...@gmail.com) wrote:

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  > 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.

>>>
>


Re: [julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-05 Thread siyu song
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  > 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. 

>>>
>

Re: [julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-05 Thread 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  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.
>>>
>>


[julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-05 Thread siyu song
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. 
>>
>

[julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-05 Thread 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. 
>


[julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-04 Thread siyu song
Oh you are right. ".==" works very well. I don't know this before. I am 
still a newcomer here. Thank you very much.

在 2016年7月4日星期一 UTC+9上午11:35:10,Cedric St-Jean写道:
>
> Maybe I'm missing something, but doesn't find(Mat_a .== Mat_b) work as in 
> Matlab? (Julia needs the dot before the ==)
>
> On Sunday, July 3, 2016 at 10:18:10 PM UTC-4, siyu song wrote:
>>
>> But intersect doesn't tell us the index of the elements in the 
>> matrix(array), I think. 
>> 在 2016年7月4日星期一 UTC+9上午3:14:54,Fred写道:
>>>
>>> intersect(a,b)
>>> union(a,b)
>>>
>>> Le dimanche 3 juillet 2016 19:54:02 UTC+2, siyu song a écrit :

 In MATLAB, it's easy to do this by using : find(Mat_a == Mat_b). Is 
 there an easy similar way to do this? 

>>>

[julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-03 Thread Cedric St-Jean
Maybe I'm missing something, but doesn't find(Mat_a .== Mat_b) work as in 
Matlab? (Julia needs the dot before the ==)

On Sunday, July 3, 2016 at 10:18:10 PM UTC-4, siyu song wrote:
>
> But intersect doesn't tell us the index of the elements in the 
> matrix(array), I think. 
> 在 2016年7月4日星期一 UTC+9上午3:14:54,Fred写道:
>>
>> intersect(a,b)
>> union(a,b)
>>
>> Le dimanche 3 juillet 2016 19:54:02 UTC+2, siyu song a écrit :
>>>
>>> In MATLAB, it's easy to do this by using : find(Mat_a == Mat_b). Is 
>>> there an easy similar way to do this? 
>>>
>>

[julia-users] Re: How can I find the common elements of two matrix(or arrays)?

2016-07-03 Thread Fred
intersect(a,b)
union(a,b)

Le dimanche 3 juillet 2016 19:54:02 UTC+2, siyu song a écrit :
>
> In MATLAB, it's easy to do this by using : find(Mat_a == Mat_b). Is there 
> an easy similar way to do this? 
>