Array equivalent to MatLab's 'find' function

This is a discussion on Array equivalent to MatLab's 'find' function within the C Programming forums, part of the General Programming Boards category; I'm converting MatLab code into c. For MatLab, the code is: Code: for (ii = 1:size(A,2)) index = find(A:,ii) > ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Array equivalent to MatLab's 'find' function

    I'm converting MatLab code into c.
    For MatLab, the code is:

    Code:
    for (ii = 1:size(A,2))
       index = find(A:,ii) > -Inf);
       disp(index);
    end;
    where A is the matrix. The output would give you the row number in each column of the matrix where the value is greater than -Inf.

    So far, I have written code that will do all of this except for one thing...store it CORRECTLY as a new matrix. The code I figured out so far is as follows...


    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	int i, j, size_A=2, size_A2=3;
    	float A[2][3]= {{-1e300, 2, 3},{2,-1e300,5}}; /*NOTE: -1e300 outputs as -Inf*/
    	int size_index=0;
    	int rloc, cloc;
    	float index[1][size_index];
    	
    	for(j=0; j<size_A2; j++) /*Finds rows where values in each column are greater than -Inf. Also finds length of new linear matrix*/
    	{
    		for(i=0; i<size_A; i++)
    			{
    				if(A[i][j]>-1e300)
    				{
    					cloc=j+1;
    					rloc=i+1;
    			
    		
    			size_index++;
    			index[1][size_index]= rloc;
    			printf("col:%d\nrow:%d\nsize:%d\n", cloc, rloc, size_index);
    
    
    				}printf("%d", index[1][size_index]);
    			}
    	}
    
    	return 0;
    }
    I used printf to make sure the values of the column, row, and size of the new matrix are all correct. However, the second printf is supposed to print the stored matrix, but my compiler goes haywire. The output should be something like...

    2 1 1 2

    for this particular matrix.
    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > size_index++;
    > index[1][size_index]= rloc;
    You can't grow variable length arrays dynamically. To do this, declare index as a pointer instead, then use realloc() to grow the size:
    Code:
    float *index;
    Or make index a fixed size:
    Code:
    float index[size_A*size_A2];

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Thanks for your help. I also figured out that since index will be a linear matrix, it doesn't make sense to put the [1] for the first dimension.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. a doubt about sending an array to a function
    By louis_mine in forum C Programming
    Replies: 13
    Last Post: 05-14-2005, 11:50 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. can u help me with pointers?plzzzzzzzz
    By itcs in forum C Programming
    Replies: 7
    Last Post: 07-11-2003, 01:29 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21