0

I have a stupid problem, I can't find the answer to ^^.

I have a 100x10000 double matrix containing integers from 1 to 4 and want to find row-wise the column-count between every single integer

My first idea was to use:

storage_ones = cell(100,1); 

     for n = 1:100; 
       [row col] = find(matrix(n,:)==1);
       storage_ones{n,1} = col;
     end

And then substract them in another loop. But with find I get following Answer:

Empty matrix: 1-by-0

Does anybody have an idea how I can solve this problem?

Thanks in advance!!

  • 1
    You likely are suffering from floating point errors where `matrix` is never *exactly* equal to `1`. Instead look for where `matrix` is *close* to `1`: `abs(matrix(n, :) - 1) < 1e-12`. If you matrix really only contains integers, consider using an integer datatype such as `uint8` – Suever Jul 19 '16 at 18:39
  • Also it's possible that you just don't have any 1's. – Suever Jul 19 '16 at 18:48
  • Did you try using the `histc` function? `[a, b] = histc(matrix', 1:4);` – Prakhar Jul 19 '16 at 19:29

1 Answers1

0

Your issue is potentially due to one of two things:

  1. Since you're using a double datatype, it is possible that you're encountering floating point errors where the values aren't going to be 1 exactly. If this is the case consider not checking for exact equality and instead check if it is very close to 1 using a small epsilon (here I used 1e-12).

    [row, col] = find(abs(matrix(n,:) - 1) < 1e-12);
    

    If you really have an integer datatype, consider using uint8 to store your data rather than double and then you can perform exact comparisons.

    matrix = uint8(matrix);
    
    % Then for your comparison
    find(matrix(n,:) == 1)
    
  2. You may just not have any 1's in that column. If find can't find any matches, it returns an empty array.

    find([1 2 3] == 4)
    %   Empty matrix: 1-by-0
    
Community
  • 1
  • 1
Suever
  • 64,497
  • 14
  • 82
  • 101