Dear all,
Here is part of my code for vectorizing the for loop and if statement:
u=zeros(N,1);
du=zeros(n,1);
j=1:1:n
du(rem(j,5)==1)=u(j+1)-u(N+(j-rem(j,5));
du(rem(j,5)~=1)=u(j+1)-u(N+(j-rem(j,5)-1);
but i got the following error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Can anyone point out the error for me? Thank you.

 採用された回答

Walter Roberson
Walter Roberson 2013 年 12 月 2 日

0 投票

j5 = rem(j,5);
jsel = j5 == 1;
jnsel = ~jsel;
du(jsel) = u(j(jsel) + 1) - u(N + j(jsel) - j5(jsel));
du(jnsel) = u(j(jnsel) + 1) - u(N + j(jnsel) - j5(jnsel) - 1);
Alternately:
j5 = rem(j,5);
jnsel = j5 ~= 1;
du = u(j + 1) - u(N + j - j5 - jnsel);

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2013 年 12 月 2 日

回答済み:

2013 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by