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일

1 개 추천

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

Samuel
Samuel 2011년 10월 11일
okay, so the count=count+1 is the only way to increment it?
I will wait and see if i see other answers, and if not, I will accept your answer. Thanks!
Jan
Jan 2011년 10월 11일
@Samuel: count=count+1 *is* the only straight method. It is efficient in Matlab.
Pierre
Pierre 2018년 1월 18일
It is not efficient, especially when "count" is a variable like
mycountcell{individual,session,variable}(1:end-1,:,:,1)
but it's the only method Matlab knows.
Luca Amerio
Luca Amerio 2018년 4월 10일
Completely agree with Pierre. When "count" is a long string, this gets very confusing.
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일

17 개 추천

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

Walter Roberson
Walter Roberson 2020년 3월 23일
MATLAB's parser is hand-crafted rather than table driven or generated out of something like BNF, which makes it sometimes fragile for adding new operators.
In the last decade or so, MATLAB has added only two new operators:
  • the ~ operator for input and output arguments to indicate that the corresponding value is unused or to be discarded. That was, IIRC, R2009a.
  • The " operator for string data type, in R2017a, after introducing the data type in R2016b. It took them until R2019b to get in into the rest of the language. I am not sure that Simulink has fully caught up yet.
Syntax changes for non-operators has been more rapid; in particular there have been changes in class definitions. But they are very conservative about new operators.
Alexandre Sagona
Alexandre Sagona 2023년 3월 19일
1000€ to not have all operators other langages have. Love to use sprintf also
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.

댓글을 달려면 로그인하십시오.

카테고리

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

질문:

2011년 10월 11일

댓글:

2024년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by