1

I have two arrays x and y of size 4013. The values of x array are row indices and of y array are column indices. I want to create a matrix of size 512*512 and insert values using x and y indices. How can I do this ?

4
  • Those x and y would act as the indices, but what values would you put in there in the matrix and what about the indices of the matrix that won't be indexed with them? Commented Apr 19, 2014 at 14:45
  • I am dealing with an gray scale image(matrix of 512*512). At first i have extracted those pixel locations having value 255(logical 1) using find() function from the image in x and y array. x contains row value of that pixel and y contains column value. Now i want to create a new matrix and put 255(logical 1) value in the row and column location of x and y, other location values will be 0. Commented Apr 20, 2014 at 9:48
  • Use @Naveh's solution with val=255; Commented Apr 20, 2014 at 9:53
  • I missed this comment about the 255 value earlier. Edited the answer now. Commented Apr 22, 2014 at 11:09

1 Answer 1

2

You can use sub2ind to turn index vectors into matrix indices. For example:

x = randi(512, 4000, 1);
y = randi(512, 4000, 1);
val = 255;
mat = zeros(512, 512);
mat(sub2ind(size(mat), y, x)) = val;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.