How can I replace for loop with meshgrid?

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.

답변 (1개)

madhan ravi
madhan ravi 2019년 3월 23일
편집: madhan ravi 2019년 3월 23일

1 개 추천

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2016b

질문:

2019년 3월 23일

편집:

2019년 3월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by