Thread: how to search for something in the matrix which is( 2-D array)

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    how to search for something in the matrix which is( 2-D array)

    I want to know using either C/MATLAB , how to scan the image. I want to find out if there is any number of ones (1s) exist in the matrix.
    For example:
    Say, A matrix called M is 3x3. Now if M is a binary matrix:
    M = [ 0 0 1; 0 0 0; 1 1 0];
    By observing the matrix M, we know that there are three number of ones(1’s) in the matrix, right? I think there is some kind of code we can use: using for loops, if statements , and so on. I think the concept is simple that is if you see any 1’s in the matrix; send some type of message as an output.
    HINT: we know the size of the matrix: So,
    // size of the matrix M is: // size (M)
    // int x=size of matrix;
    // for ( int i=0,  size of matrix)
    // using if statement // we are using the condition that if ( 1) is found in the matrix,// send some type of message as an output may be a cout string.
    Let me know if you understand the question, please.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think there is some kind of code we can use: using for loops, if statements , and so on.
    Yep.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const int rows = 3;
    	const int columns = 3;
    
    	int M[rows][columns] = 
    	{
    		{0,0,1},
    		{0,0,0},
    		{1,1,0}
    	};
    
    	for(int i = 0; i < rows; i++)
    	{
    		for(int j = 0; j < columns; j++)
    		{
    			if(M[i][j] == 1)
    			{
    				cout<<"row "<<i<<" column "<<j<<" : "<<1<<endl;
    			}
    		}
    	}
    
    		
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with matrix transpose
    By sass208 in forum C Programming
    Replies: 6
    Last Post: 12-09-2006, 02:02 AM
  2. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. binary search in an array
    By brianptodd in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2002, 02:05 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM