Find the average point with the four points surround it, using For-loop and function
Mostrar comentarios más antiguos
Hi guys,
I had trouble on getting the average point with the four points surround it. Matlab said there is a error on a(i,n) = 0.2*(a(i-1,n) + a(i+1,n) + a(i,n+1) + a(i,n-1) + a(i,n)) and I can't get rid of it. Can anyone help me up with that? And also, for setting the four boundaries of the array, a, to zero, I use the zeros() function, I think I might also get wrong with that. I made some comment on it and hope it helps.
function averahepoint(k,n)
% k is the number of time steps
% n is the size of a square n x n matrix
a = peaks(n); % make an arbitrary square matrix
a = zeros(k); % Set the four boundaries of the array, a, to zero.
% It means every element in the array where the first index equals 1 or n,
% or the second index equals 1 or n must be set equal to zero.
% A loop that performs statements the number of times you entered as an input to your
% function.
for i = 1:k
a(i,n) = 0.2*(a(i-1,n) + a(i+1,n) + a(i,n+1) + a(i,n-1) + a(i,n))
% average each point in array a excluding the points on the boundaries with the four points
% that surround it
% For example a(23,60) = 0.2*(a(22,60) + a(24,60) + a(23,61) + a(23,69) + a(23,60))
disp(i)
if mod(i,10)==0 % Every 10 iterations ,make a filled contour plot of the array
disp('Hello')
end
end
end
2 comentarios
Adam
el 18 de Abr. de 2016
n is the size of your a array so trying to index into it with n + 1 will clearly be outside the range of the array. I'm not really sure what you are aiming to do with that statement.
a = zeros(k)
will just totally overwrite the matrix you created on the line above it though too so if k < n that would be another reason for the failure.
david Chan
el 18 de Abr. de 2016
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!