MATLAB Answers

Multiple for loops in explicit fashion with time step

19 views (last 30 days)
Jhon Paul
Jhon Paul on 12 Jan 2021
Commented: Jhon Paul on 13 Jan 2021
Is there any efficient way to create Multiple for loops in explicit fashion with time step. If I have to keep a convergence criteria what shall be the approach ?

  0 Comments

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 12 Jan 2021
Edited: Walter Roberson on 12 Jan 2021

  2 Comments

Walter Roberson
Walter Roberson on 12 Jan 2021
Sometimes when people talk about multiple for loops, what they want is to read corresponding values from vectors. For example,
x = randn(1,10);
y = rand(size(x));
z = zeros(size(x));
for K = 1 : numel(x)
z(K) = x(K) + y(K);
end
The above form of incrementing an index and using the index to retrieve corresponding values is the recommended form.
But sometimes people don't want to have to keep indexing the variables, and want simple unindexed variables to be updated with each iteration, sort of like
X = randn(1,10);
Y = rand(size(X));
z = zeros(size(X));
for K = 1 : numel(X)
x = X(K);
y = Y(K);
z(K) = x + y;
end
except they want to do that without having to do all of those assignments like x = X(K).
Sometimes people try to write it out like
X = randn(1,10);
Y = rand(size(X));
z = [];
for x = X, y = Y
z = [z, x + y];
end
hoping that the syntax will step through X and Y in parallel, pulling out corresponding pieces. However, MATLAB does not have such a syntax.
MATLAB does have a little-used syntax:
X = randn(1,10);
Y = rand(size(X));
z = [];
for xy = [X; Y]
x = xy(1); y = xy(2);
z = [z, x + y];
end
What is happening here is that when you do a for loop, what is really being looped over is the columns of the right hand side. It is just that almost always, there is only one column and so the loop control variable becomes a scalar at each point. But if you know the seldom-used syntax and do not mind confusing people who do not have a lot of MATLAB experience, then you can deliberately create column.
But how can you do it without having those x = xy(1) and so on? Well, you can be even more obscure and use
X = randn(1,10);
Y = rand(size(X));
z = [];
for xy = num2cell([X;Y])
[x, y] = xy{:};
z = [z, x + y];
end
You can do it. Is it efficient? No. Is it clear to most readers? No. Will MATLAB optimize it well? No.
Does it get you L33t points for use of studied obscurity? Maybe... but it will not get you points from managers who understand that testing and maintenance and enhancement of code is often far more costly than writing new code, and so prefer the simplest clearest code that meets the performance requirements.
Jhon Paul
Jhon Paul on 13 Jan 2021
Dear Mr. Walter thank you very much for your enlightenning explaination . I don't have words to express my gratitude further. Its really a masterpice writing, which will be hard to get through any other source. I will be posting my code, based on this reading. Will be fortunate, if you can stay connected and go through my code . Im sure my code will have 100 of errors !!
Thanking you once again. Stay safe stay blessed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by