You can't do it for randomly generated matrices, because you have to ensure that matrix A
has lines and columns as required from the values of x
and y
.
In this case, you can write:
for i=1:length(x(:))
B(i)=A(x(i),y(i));
end
B=reshape(B,size(x));