for loop like c++ in MATLAB

for loop in C or C++ is very flexible and fast. Why MATLAB doesn't create a for loop like C or C++?

댓글 수: 1

Hamid
Hamid 2014년 1월 25일
편집: Hamid 2014년 1월 25일
In general, problem of MATLAB is in for loops that cause some slowness. If while loops are used instead of for loops, speed will be increased very much (some complicated tests can be very interesting) and additionally, while loops are more flexible than for loops and therefore, MATLAB, will be an excellent friend for a programmer with very complicated mathematical calculations and operations. Additionally, because of existence of MATLAB Coder toolbox, it's unnecessary to follow C++ for these complicated calculations. In fact, in early future, C++ with respect to MATLAB, will be like Assembly with respect to C++. Only MATLAB. Thanks God for such a powerful software and I wish God's attentions and helps for its kind engineers.

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

답변 (2개)

Image Analyst
Image Analyst 2014년 1월 12일

1 개 추천

I'm not sure I understand what you are saying. MATLAB does have a for loop. Sure it doesn't use a { and the } is replaced by "end" and the way your specify the iterator is different, but it still has one. Different languages have different syntax - that's just the way it is, not just for MATLAB but for all languages.

댓글 수: 11

Hamid
Hamid 2014년 1월 13일
편집: Hamid 2014년 1월 15일
I love matlab; it's excellent but syntax isn't in question at all. suppose we want to detect numbers holding in Pythagorean theorem, a^2=b^2+c^2 , from 1 to 1000 by three consecutive while or for loops. after detecting some elements, we should reject detected elements. one way is inserting zero for them (we use an if statement before executing inner for or while loop for rejecting detected elements). this work, takes very long time even by using mex-file correspondent but if we could say, after detecting first three elements, to loop that we determined these three numbers and go directly to first for or while loop to start from beginning, it takes very smaller time. I love matlab but it is not good in some loops and optimizing for and wile loops, will answer very people that, for example, say that matlab isn't suitable for performing very complicated mathematical expressions involved in physical chemistry that last days, months or even years. I wish that day, performing all my complicated mathematical calculations in matlab.
José-Luis
José-Luis 2014년 1월 13일
편집: José-Luis 2014년 1월 13일
I am not sure I understand what you mean. That being said, I like Matlab, but it can be rather sluggish. It's the price you pay (besides what the Mathworks charges) for a high-level language. Also, it is an interpreted language, so there's an overhead right there.
If you write decent C++, then Matlab is not going to beat that.
The question remains whether you can write fast code in another language and you are willing to invest the time it takes. You might have to go down to assembly if you want blazing fast, but then you have to be confident that you can do a better job than an optimizing compiler.
Image Analyst
Image Analyst 2014년 1월 13일
I don't see how a^2=b^2+c^2 is going to take 3 loops. Two maybe, if you want every possible combination of every b with every c, but how do you get three? What is being incremented in each loop?
Hamid
Hamid 2014년 1월 15일
I mean Pythagorean theorem. Of course, it is immaterial here; subject is another thing: ability to return to first loop in several loops written sequentially and changing their indices.
Sure. Use:
break
for ii = 1:10
for jj = 1:10
if jj>5
break
end
end
disp([ii jj])
end
Nothing you've described is not readily available and fast in MATLAB.
Then don't use a for loop. A while statement should do the trick
ii=1;
while ii < 10
ii = ii + 1;
if something
ii = whatever iteration you want to go back to
do stuff
ii = you can even jump forward
end
end
Hamid
Hamid 2014년 1월 16일
편집: Hamid 2014년 1월 16일
for one loop, while is very easy and good but when we have 3 loops, for example, we cannot say to first loop anything from third loop. the problem is here.
Image Analyst
Image Analyst 2014년 1월 16일
I didn't really understand what "we cannot say to first loop anything from third loop" means. I think we're all waiting for you to give us a loop in C and a loop in MATLAB (that does the same thing if you can), and show how what you want to do is much easier in C than in MATLAB.
Hamid
Hamid 2014년 1월 25일
I think, because of existence of MATLAB Coder toolbox, it's unnecessary for me to follow C++. What a Great Chance. In fact, in early future, C++ with respect to MATLAB, will be like Assembly with respect to C++.
Image Analyst
Image Analyst 2014년 1월 25일
Well it looks like we're done here, but I don't think you illustrated your point about how C is easy but MATLAB is harder. You gave us no code examples to compare them.
Hamid
Hamid 2014년 1월 25일
편집: Hamid 2014년 1월 25일
In general, problem of MATLAB is in for loops that cause some slowness. If while loops are used instead of for loops, speed will be increased very much (some complicated tests can be very interesting) and therefore, MATLAB, will be an excellent friend for a programmer with very complicated mathematical calculations and operations. Additionally, because of existence of MATLAB Coder toolbox, it's unnecessary to follow C++ for these complicated calculations. In fact, in early future, C++ with respect to MATLAB, will be like Assembly with respect to C++. Only MATLAB. Thanks God for such a powerful software and I wish God's attentions and helps for its kind engineers.

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

Azzi Abdelmalek
Azzi Abdelmalek 2014년 1월 12일
편집: Azzi Abdelmalek 2014년 1월 12일

0 개 추천

Each language has its advantages and disadvantages, but sometimes, if the user doesn't know how to use it, doesn't mean the language is bad. In your case maybe, if you give an example, there should be a good way to handle it either with c++ or with matlab

댓글 수: 1

Hamid
Hamid 2014년 1월 13일
I love matlab; it's excellent but syntax isn't in question at all. suppose we want to detect numbers holding in relation: a^2=b^2+c^2 from 1 to 1000 by three consecutive while or for loops. after detecting some elements, we should reject detected elements. one way is inserting zero for them (we use an if statement before executing inner for or while loop for rejecting detected elements). this work, takes very long time even by using mex-file correspondent but if we could say, after detecting first three elements, to loop that we determined these three numbers and go directly to first for or while loop to start from beginning, it takes very smaller time. I love matlab but it is not good in some loops and optimizing for and wile loops, will answer very people that, for example, say that matlab isn't suitable for performing very complicated mathematical expressions involved in physical chemistry that last days, months or even years. I wish that day, performing all my complicated mathematical calculations in matlab.

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

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2014년 1월 12일

편집:

2014년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by