0

I am creating a task in psychtoolbox and I need a grating for that. I made the grating and it is working well, but now I just want to cut some pieces out of it by setting those matrix elements to zero (currently I am using a mask instead, but I want to cut out pieces in the source, the grating itself).

To cut out the pieces I want, I need the indexes of the grating matrix (called A), so I figured I use ind2sub. This is however not working:

[I,J] = ind2sub(size(A),A);

'I' gives the same as 'A' and 'J' gives ones(size(A))

Here you can find A

Example

Here is an example of what I'd like to do:

% A is a 3x3 matrix
% if I use "[I,J] = ind2sub(size(A),A);" I get the problem as described before: 
% I = A and J = ones(3,3)
% When I use "[I,J] = ind2sub(size(A),[1:numel(A)]);" as suggested, I do get the
% following (which is a vector but can be reshaped into a 3x3 matrix):
I = [1 1 1 2 2 2 3 3 3]
J = [1 2 3 1 2 3 1 2 3]
I = reshape(I,[3,3]);
J = reshape(J,[3,3]);

K = I + J; % = [2 3 4; 3 4 5; 4 5 6]
for i=1:length(A)
for j=1:length(A)
if K(i,j)>*some threshold*
A(I(i),J(i))=0
end
end
end
Mats
  • 1
  • 1
  • 1
    There's a lot there that doesn't make any sense ... why perform the addition I+J, why equate K = ... = , the use of B ... – Buck Thorn Mar 06 '15 at 12:17
  • What size is the matrix A you are using? – Ander Biguri Mar 06 '15 at 12:35
  • Ok, I think the problem is you are going in circles and not understanding how to use indexing. The K=I+J is not going to work for starters. I recommend you use logical indexing, like so: `mask = B>thresh; A(mask)=0;` – Buck Thorn Mar 06 '15 at 12:36

2 Answers2

0

Seems what you want are the indices i,j for each entry in your array. Then do this:

  A = ones(2,3); % <-- dummy data
  [I,J] = ind2sub(size(A),[1:numel(A)]);

then the result is

 I =

   1   2   1   2   1   2

J =

   1   1   2   2   3   3
Buck Thorn
  • 5,024
  • 2
  • 17
  • 27
  • Exactly. But when i do this I get both I and J are 1132 (which is the length of A).. – Mats Mar 06 '15 at 09:00
  • @Mats Strange, please look at the more specific example I added – Buck Thorn Mar 06 '15 at 09:10
  • hmm I tried to think of a way to do it like this, but for my purpose I think I need the indices the way ind2sub(size(A), A) should give them.. I'll give an example of what I want, see question above. – Mats Mar 06 '15 at 12:07
  • I think I found a solution, see below. Thanks a lot for your help! – Mats Mar 06 '15 at 14:13
0

Ok I think I got it the way i want it:

[I,J] = ind2sub(size(A),[1:numel(A)]);
I = reshape(I,size(A));
J = reshape(J,size(A));
K = I + J;
for i=1:length(A)
for j=1:length(A)
if K(i,j)<halfWidthOfGrid %predefined threshold
   A(i,j)=0;
end
end
end

I think I should have been more clear about what I want to cut out of the grating. I want to cut off all the edges (so make it more like diamonds in playing cards). Since I want to cut off triangular shapes, I can say that the sum of 'i' and 'j' in A(i,j) should not exceed a threshold

Mats
  • 1
  • 1
  • Ok, I see now the purpose of K, that was confusing. Still, I think you can use logical indexing to streamline this: `A(K – Buck Thorn Mar 06 '15 at 14:17
  • You do some weird stuff in the loop by the way by including `j=j+1`,` i=i+1`. I am still confused by what you are trying to do – Buck Thorn Mar 06 '15 at 14:19