matlab equivalent of "+="

1,438 ビュー (過去 30 日間)
Samuel
Samuel 2011 年 10 月 11 日
コメント済み: Real User 2024 年 1 月 31 日
Hello all,
First of all, I would like to say thanks for all your help. Your prompt and accurate replies save me a lot of programming time.
When I used to program in java, there was a useful command, so to say:
"counter+="
which will increment the variable itself. This was a useful function to use in for loops. I was wondering if matlab had a shortcut expression such as this, something that I can use in an array even, so like
somearray[i]+=; can work? Or is the only available option to say counter=counter+1? I just want to see if there was a faster way to write the expression.
thanks!
Sam

採用された回答

Sean de Wolski
Sean de Wolski 2011 年 10 月 11 日
for-loops automatically increment and any reassignment inside the loop will be reset at the next increment:
for ii = 1:10
disp(ii)
ii = 5000;
end
To be able to toy with the counter use a while loop and to the assignment manually
count = count+1;
  6 件のコメント
Adam
Adam 2018 年 4 月 10 日
It's annoying to type, but why is it 'not efficient'?
Ilya Gurin
Ilya Gurin 2020 年 7 月 10 日
It's annoying both to type and to read.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 10 月 11 日
MATLAB does not have ++ or += or -- or -= or *= or /= or %= and is not expected to get any of those for some time (if at all.)
I think if you look back at your Java you will find it was ++ rather than += that you were using for incrementing.
  4 件のコメント
Andrew
Andrew 2024 年 1 月 18 日
@Alexandre Sagona Strong agree; I wish my MATLAB class at uni would just let me use C instead.
Real User
Real User 2024 年 1 月 31 日
data(complex_index_sequence, another_one) = data(complex_index_sequence, another_one) + data2
would benefit from "+=". It would be much easier to read and maintain.
It helps a bit to define the indices separately before that line, but "+=" would be better.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by