0

In matlab, after meeting a specific criterion, I used to return back the pixel itself and store it in the vector pixels as follows:

pixels(index) = y(i,j);

Now, I would like to return the location of those pixels. Should I do the following?

pixels(index) = i,j;

EDIT

If I want to then set those indexes to the value 1, I do the following, right?

for i=1:m
for j=1:n
y(i,j)=1
end
end

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 1
    Both the location and the set tag do not apply here. Furthermore your second question is so basic that it makes me wonder whether you even tried to solve it yourself. – Dennis Jaheruddin Feb 21 '13 at 17:48
  • @Dennis Jaheruddin. For the second part I think it would be simply something like this: `for i=1:n / for j=1:m / y(i,j) = 1`. What about the first part of the question? – Simplicity Feb 21 '13 at 17:57

2 Answers2

2

It is extremely inefficient to do so in a nested loop in Matlab.

Using sub2ind can help you do so much faster:

y( sub2ind( size(y), i, j ) ) = 1;

EDIT - sub2ind

What sub2ind does?
Suppose you have a matrix M of size [4 6]:

M = [ 1  5  9  13  17  21
      2  6 10  14  18  22 
      3  7 11  15  19  23
      4  8 12  16  20  24 ];

You wish to access two elements: the one at the first row and second column, and another at the fourth row and the fifth column. In that case you have the rows you wish to access r = [ 1 4 ] and the columns you wish to access c = [ 2 5 ]. However, if you try and access

>> M( r, c )

This is a 2x2 matrix

ans = 
5  17
8  20

And not the two elements you were looking for (which are 5 and 20).

What sub2ind does is convert the row/column indices you have into linear indices

>> sub2ind( size(M), r, c )
ans =
5 20

which happens to be the linear indices of the requested entries.

You can think of linear indices as the single index required to access an element in a matrix in the case that the matrix was converted to a vector stacking its columns one after the other.

A few comments:

  1. Matlab has a few ways of indexing matrices: by row / column indices (like i and j in your question). By linear indices (like index in your question). However, the more efficient way is to use logical indexing: that is, using a matrix of the same size as y with true for the entries you wish to set / get.
    So, in your example, if you could get such a logical matrix instead of index or i and j it would have been better.

  2. Matlab has many advantages over other programing languages. One of them is its ability to perform vector/matrix operations extremely efficient. Resorting to loops, or worse, nested loops, is something that should be avoided in Matlab.

  3. It is not a good practice to use i and j as variables in Matlab.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thanks for your reply. I tried to understand what `sub2ind` means but wasn't clear. So, if I want to set locations of pixels that meet a specific criterion I should only use this: `y( sub2ind( size(y), i, j ) ) = 1;`. Can you kindly describe what `sub2ind` does in this context? Thanks – Simplicity Feb 21 '13 at 18:22
  • @Med-SWEng - I added an explanation on `sub2ind` – Shai Feb 21 '13 at 18:33
  • `sub2ind` will only give you the linear indices, not the values of `y`. In my example I made the entries of `M` equals their linear indices - that might have confused you. – Shai Feb 21 '13 at 20:08
  • Got what you mean. In my function actually I pass two matrices. The first matrix `x` represents the image, and the second matrix `y` represents the degrees of membership of each pixel in `x` to some region for example, and values here are between `0` & `1`. When I find that a pixel for instance has a degree of membership = 1, I want to set that pixel (in `x`) to `1`. How can I do that using `sub2ind`? Thanks – Simplicity Feb 21 '13 at 20:26
  • @Med-SWEng you don't need `sub2ind` for that. You ca simply do `y( x == 1 ) = 1` this is using _logical indexing_ of the logical matrix `x==1` – Shai Feb 22 '13 at 07:55
1

If you want to find the occurrence of a value y(i,j) simply evaluate

idx = (pixels == y(i,j));

Depending on your variables you can then probably do

index(idx) = 1;
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122