MATLAB Answers


how to use nested for loop by grabbing one column and subtracting another column.

Asked by tonyrodriguez on 29 Mar 2018
Latest activity Edited by Roger Stafford on 29 Mar 2018
so i have this huge file that is 1478 by 1236 matrix and i want to calculate the xslope and yslope as follows

  1 Comment

so far i have this
cellsize = 9.47733915533982;
xslope = zeros(1478,1236);
n = length(xslope);
for i = 1:n for j = 1:1:n
xslope(i,j) = ( z(i,j+1)-z(i,j) )/cellsize;
it tells me i have a matrix dismension.. can someone help me fix this or tell me how i can do it?

Sign in to comment.


1 Answer

Answer by Roger Stafford on 29 Mar 2018
Edited by Roger Stafford on 29 Mar 2018

Your n value will be 1478, but in z(i,j+1) you have allowed the j+1 index to get as large as 1479 because you have "for j=1:1:n" and its maximum value should be only 1236. Hence the error message. You need to do
[n,m] = size(z)
for j = 1:m-1


Sign in to comment.