MATLAB: How to obtain the row and column indices of a “bsxfun” matrix

matrices

I have a code as follows:
A=rand(1,100);
B=rand(1,1000);
idx=bsxfun(@minus,A(:,1),B(:,1)')<0.2;
I want to make a matrix C, in which each cell gets a value if the corresponding cell of "idx" is 1. I wrote the following code:
C=zeros(100,1000);
C(idx)=C(idx)+array_1(1,i)+array_2(1,j);
In which array_1 and array_2 are two row vectors. Furthermore, "i" is the row index of each nonzero element of "idx", and "j" is the column index of each nonzero element of "idx". In other words, in each nonzero cell of "idx", I should know the row and column indices separately, so that array_1 and array_2 can be applied.
I know that the last line might be incorrect, but I don't know how to correct that. Any comment or hint is really appreciated!
Thank you!

Best Answer

Use IND2SUB or FIND with the two output option. By the way, you do realize that idx is scalar as you have it, right? What you may want is:
A=rand(1,100);
B=rand(1000,1);
idx=bsxfun(@minus,A,B)<0.2;
which we can make directly:
idx = (rand(1000,100)-rand(1000,100))<.2;