How can I change non zero values of a matrix into a value that increments for each element it is applied to.

2 views (last 30 days)
I have a matrix A filled with zero's and non zero values. I wish to change all the non zero values of A into a unique number. To do this I thought of changing the first non zero value to 1 then the next to 2.. and so on until the end of the matrix is reached. I'm new to Matlab and not sure how to go about doing this.
I'm aware that I can use
A(A!=0) = [x]
But I'm not sure how to set x up so it's a unique value each time.
Thanks.
  2 Comments
Jake Murkin
Jake Murkin on 6 Sep 2018
The matrix represents the blue value of the RGB of an image. An example would be like this:
A = [0 0 0 0 0 0;
0 0 0 0 0 23;
0 0 0 0 23 24;
0 0 0 24 25 27;
0 0 0 0 23 25;
30 32 0 0 0 0;
29 0 0 0 0 0;]
The matrix I'm working with is around 200x200 so this is just an example.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 6 Sep 2018
A(~A) = 1:sum(~A(:));
would do it.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!