I have some large matrices of data, and a a two column matrix containing x and y locations, is there an easier way to work with the data elements corresponding to those points then:
for adj = 1:size(loc,1)
testFunc(data1(loc(i,2),loc(i,1)), data2(loc(i,2),loc(i,1)), othervals)
end
Mostly I’m looking for a way to access the data elements by something closer to data1(loc(i))
What you want is to access elements of
datavia their linear indices. Linear indices increment first along the first dimension, then along the second dimension, and so on. For example the elements of a 3-by-2 array would be addressed in the following orderSo to get element
(2,1)of a 2-by-3 array via linear indexing, you would callarray(3). To convert between linear index and subscripts (such as the pair2,3), you can useind2subandsub2ind, respectively.In your case, you’d run
if the first column of
locindexes into columns ofdata, and the second column oflocindexes into rows.Then you can loop over
linIdxto change your function call inside the loop to