AND Operation on a two dimensional array
Question posted by: Mugunth
(Guest)
on
January 11th, 2008 02:35 AM
What is the most effective way of implementing an AND operation on a
two-d array.
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
..
..
..
The result should be another array that contains only 6.
Any ideas?
8
Answers Posted
Mugunth wrote:
Quote:
Originally Posted by
What is the most effective way of implementing an AND operation on a
two-d array.
What does that mean to perform AND on an array?
Quote:
Originally Posted by
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
.
.
.
>
The result should be another array that contains only 6.
Why?
Quote:
Originally Posted by
Any ideas?
Nope.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Jan 10, 6:26 pm, Mugunth <mugunth.ku...@gmail.comwrote:
Quote:
Originally Posted by
What is the most effective way of implementing an AND operation on a
two-d array.
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
The result should be another array that contains only 6.
It seems like you are trying to find a list of numbers which are in
every row of the 2-D array. Is that so?
Regards,
Ivan Novick
On Jan 11, 2:26 am, Ivan Novick <i...@0x4849.netwrote:
Quote:
Originally Posted by
On Jan 10, 6:26 pm, Mugunth <mugunth.ku...@gmail.comwrote:
>
Quote:
Originally Posted by
What is the most effective way of implementing an AND operation on a
two-d array.
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
The result should be another array that contains only 6.
>
It seems like you are trying to find a list of numbers which are in
every row of the 2-D array. Is that so?
>
Regards,
Ivan Novick
O(n^n)
O__O
Mugunth <mugunth.kumar@gmail.comwrote in comp.lang.c++:
Quote:
Originally Posted by
What is the most effective way of implementing an AND operation on a
two-d array.
With the bitwise AND operator.
Quote:
Originally Posted by
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
Since you've given a poor explanation, I can only guess that that's
equivalent to:
T arr[3][5] = { {1,3,5,6,7},{2,4,5,6,1},{8,6,4,9,2} };
Quote:
Originally Posted by
The result should be another array that contains only 6.
This one's easy:
T arr[1] = {6};
Quote:
Originally Posted by
Any ideas?
A better explanation might beg better replies.
--
Tomás Ó hÉilidhe
"Diego Martins" <jose.diego@gmail.comwrote in message
news:204774a2-c46c-4eca-9437-648cc7ebfe20@j78g2000hsd.googlegroups.com...
Quote:
Originally Posted by
On Jan 11, 2:26 am, Ivan Novick <i...@0x4849.netwrote:
Quote:
Originally Posted by
>On Jan 10, 6:26 pm, Mugunth <mugunth.ku...@gmail.comwrote:
>>
Quote:
Originally Posted by
What is the most effective way of implementing an AND operation on a
two-d array.
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
The result should be another array that contains only 6.
>>
>It seems like you are trying to find a list of numbers which are in
>every row of the 2-D array. Is that so?
>>
>Regards,
>Ivan Novick
>
O(n^n)
>
Did you mean O(n^2)? Also, I don't think I get your point.
What algorithm are you considering employing which has that cost? Under
certain conditions, you can answer this question by going through the matrix
just once (but I can never remember if that's O(n), where n is the size of
the array, or O(w*h) where w and h are the width and height).
-Howard
Mugunth wrote:
Quote:
Originally Posted by
What is the most effective way of implementing an AND operation on a
two-d array.
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
.
The result should be another array that contains only 6.
>
Any ideas?
You're not explaining exactly what it is your are trying to accomplish, but
it seems like a non optimal soulution would be:
sort each row
store the first row in an array we'll call working
go through each row and remove from working any value that is not in the row
return whatever is left
You are actually looking for a subset. The subset of the elements that are
in each set (row).
Is this the case?
--
Jim Langston
Join Bytes!
On Jan 15, 3:13 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
Quote:
Originally Posted by
Mugunth wrote:
Quote:
Originally Posted by
What is the most effective way of implementing an AND operation on a
two-d array.
I've an array like,
1 3 5 6 7
2 4 5 6 1
8 6 4 9 2
.
The result should be another array that contains only 6.
Quote:
Originally Posted by
Quote:
Originally Posted by
Any ideas?
Quote:
Originally Posted by
You're not explaining exactly what it is your are trying to
accomplish, but it seems like a non optimal soulution would
be:
sort each row
store the first row in an array we'll call working
go through each row and remove from working any value that is
not in the row return whatever is left
Not sure how you can speak about a solution if you (admittedly)
don't know what the problem is:-). If it's what you think, then
sort each row, then use std::set_intersection. Something like:
typedef std::vector< int >
Row ;
typedef std::vector< Row >
Table ;
Row
findCommonElements(
Table::const_iterator
begin,
Table::const_iterator
end )
{
Row result ;
Table::const_iterator
current = begin ;
if ( current != end ) {
result = *current ;
std::sort( result.begin(), result.end() ) ;
++ current ;
}
while ( current != end ) {
Row tmp1( result ) ;
Row tmp2( *current ) ;
std::sort( tmp2.begin(), tmp2.end() ) ;
result.clear() ;
std::set_intersection(
tmp1.begin(), tmp1.end(),
tmp2.begin(), tmp2.end(),
std::back_inserter( result ) ) ;
++ current ;
}
return result ;
}
Quote:
Originally Posted by
You are actually looking for a subset. The subset of the
elements that are in each set (row).
Quote:
Originally Posted by
Is this the case?
Your description sounds more like a set intersection, treating
each row as a set.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze wrote:
Quote:
Originally Posted by
On Jan 15, 3:13 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
Quote:
Originally Posted by
>Mugunth wrote:
Quote:
Originally Posted by
>>What is the most effective way of implementing an AND operation on a
>>two-d array.
>>I've an array like,
>>1 3 5 6 7
>>2 4 5 6 1
>>8 6 4 9 2
>>.
>>The result should be another array that contains only 6.
>
Quote:
Originally Posted by
Quote:
Originally Posted by
>>Any ideas?
>
Quote:
Originally Posted by
>You're not explaining exactly what it is your are trying to
>accomplish, but it seems like a non optimal soulution would
>be:
>sort each row
>store the first row in an array we'll call working
>go through each row and remove from working any value that is
>not in the row return whatever is left
>
Not sure how you can speak about a solution if you (admittedly)
don't know what the problem is:-).
That's true. My solution was an algorithm of one of the possible things the
OP was trying to accomplish. The OP was not clear on exactly what he was
trying to do.
Quote:
Originally Posted by
If it's what you think, then
sort each row, then use std::set_intersection. Something like:
>
typedef std::vector< int >
Row ;
typedef std::vector< Row >
Table ;
>
Row
findCommonElements(
Table::const_iterator
begin,
Table::const_iterator
end )
{
Row result ;
Table::const_iterator
current = begin ;
if ( current != end ) {
result = *current ;
std::sort( result.begin(), result.end() ) ;
++ current ;
}
while ( current != end ) {
Row tmp1( result ) ;
Row tmp2( *current ) ;
std::sort( tmp2.begin(), tmp2.end() ) ;
result.clear() ;
std::set_intersection(
tmp1.begin(), tmp1.end(),
tmp2.begin(), tmp2.end(),
std::back_inserter( result ) ) ;
++ current ;
}
return result ;
}
>
Quote:
Originally Posted by
>You are actually looking for a subset. The subset of the
>elements that are in each set (row).
>
Quote:
Originally Posted by
>Is this the case?
>
Your description sounds more like a set intersection, treating
each row as a set.
That's one possible explanation of the OP's post and the one I ran with,
asking if this was in fact what he was trying to accomplish.
--
Jim Langston
Join Bytes!
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 197,050 network members.
Top Community Contributors
|