How can I replace for loop with meshgrid?

9 visualizaciones (últimos 30 días)
Darcy Chou
Darcy Chou el 23 de Mzo. de 2019
Editada: madhan ravi el 23 de Mzo. de 2019
For example,
a = zeros(20,100);
b = rand(20,100);
for m = 2:20
for n = 2:100
a(m,n)=b(m-1,n)+1;
end
end
Using FOR loops is not efficient.
Can loops be replaced with meshgrid?
Thank you.

Respuestas (1)

madhan ravi
madhan ravi el 23 de Mzo. de 2019
Editada: madhan ravi el 23 de Mzo. de 2019
You don't need a meshgrid to do the task:
[ m, n ] = size( b );
d = zeros( m, n );
d( 2 : m, 2 : n ) = b( 1 : end - 1, 2 : end ) + 1

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by