Nice
A little better way is to use a macro to do this. Adding this code, preferably under the declaration of data2:
Code:
#define data2(x,y) data2[(x) *cols + (y)]
will let you use the index as in matlab. This is actually a common "trick" in order to define multidimensional arrays in the form of a fucntion (parenthesis instead of []). data2 will actually be a 1D array, but you will use it, like data(x,y), with a 2D indexing. This will make simpler also the allocation for example, as it is actually a 1D array than a 2D.
It is exactly the same as your code, it will just help you right faster the indexing and be far more undertandable than [x*cols +y].
Just to add, even though I said this before, that in C the index starts from 0, but in matlab it starts from 1. For just the for-loop is fine, but since you use the x,y in your matlab code, make sure that you get the desired results for x = 0 and y = 0. The same for the upper bounds that might be one less.
In any case, you can pretend that the arrays start from 1 also in C and just ignore the very first element. That will guarantee that you won't have a bug by changing the index.