๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 444000-loop-with-no-index
Loop with no index - MATLAB Answers - MATLAB Central
February 9, 2019 - It is not required that a loop index is used inside the loop: a loop can still be very useful if the results of the previous iteration are used on the next iteration, which seems to be the case for your code (see the variable Vhoward ).
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ language fundamentals โ€บ loops and conditional statements
for - for loop to repeat specified number of times - MATLAB
See Loop Index Overflow (MATLAB Coder). When the number of for-loop iterations calculated by the generated code does not match the number of for-loop iterations calculated by MATLAB, the generated MEX function produces an error at run time. See Resolve Error: Cannot Determine the Exact Number of Iterations for a Loop (MATLAB Coder). Do not use for loops without ...
Discussions

Access to for loop index
Access to for loop index. Learn more about for loop MATLAB More on mathworks.com
๐ŸŒ mathworks.com
2
0
May 6, 2011
Logical indexing without for loop
Hi all, This is probably a simple question, but I would love some input from those who can lend it. I have a vector [A] I would like to index according to a condition comparing the nth entry of ... More on mathworks.com
๐ŸŒ mathworks.com
2
0
April 19, 2016
Looping with indices that are not equally spaced
I'm trying to run a loop on a group of indices I obtained using "find". The indices will not always be consecutive. So, running a for loop like: for i = won't work. I'm trying to... More on mathworks.com
๐ŸŒ mathworks.com
3
0
June 23, 2011
Is it possible to go through the elements of an array without resorting to length in a for loop?
FOR loops work faster in the form: for k = a:b . Then the vector a:b is not created explicitly, which saves the time for the allocation. The loop index is applied as columns. So if you provide a matrix, the index is a vector: ... https://www.mathworks.com/matlabcentral/answers/1728680-is-i... More on mathworks.com
๐ŸŒ mathworks.com
2
0
May 27, 2022
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 474588-save-data-from-for-loop-without-subscript-index
Save data from for loop without subscript index - MATLAB Answers - MATLAB Central
August 2, 2019 - I want to plot h as function of t without its data being overwritten. I also want the steps in each iteration to be 0.1, so indexing will not work. The standard solution for these plots is using 'indexing' h(t). This does not work because the subscript index 't' starts with '0' and has steps of 0.1. ... Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/474588-save-data-from-for-loop-without-subscript-index#answer_385940
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 6914-access-to-for-loop-index
Access to for loop index - MATLAB Answers - MATLAB Central
May 6, 2011 - Also, If I wanted to have nested loops, how would I look at the "real" index of the outer loop while in the body of the inner? Is that even possible? Sign in to comment. Sign in to answer this question. ... There is no special variable. You have to do it yourself: ... For nested loops it's similar.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 30845-saving-values-without-index
saving values without index - MATLAB Answers - MATLAB Central
March 1, 2012 - Why do you not want to use an index? It is less efficient to just append values to an existing array, but you can: ... The situation I have is that I replace all the values above and below 'this variable'. I need to save only 1 value each time and then reset all other variables(above and below it) to zero and repeat the loop.
Find elsewhere
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable
Possible to iterate over table rows without a loop index variable? - MATLAB Answers - MATLAB Central
April 18, 2024 - Some authority suggests using rowfun on table instead of for-loop row indexing if runtime performance matters. https://www.mathworks.com/matlabcentral/discussions/general/847971-what-frustrates-you-about-matlab-2/2562386 ยท Read the comments following my complain about combinations only providee table as output format. Transpose the table as Torsen suggets or retreive the content of the table (without the tanle container) are two other work around of performance hi issue.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 499257-empty-for-loop-index-variable
Empty for loop index variable - MATLAB Answers - MATLAB Central
January 7, 2020 - The variable i (now inside another for loop meant for iterating frames) is completely blank, which never happened in the previous code. This is what the already mentioned code looks like inside the loop. Kindly note that this a truncated part of the code. ... The remainder of the code is derived from an existing program linked here (https://in.mathworks.com/matlabcentral/fileexchange/47726-video-processing-tutorial)
Top answer
1 of 2
5

The documentation in for() states:

for index = values
   ...
end

where values has one of the following forms:

  • ...

  • valArray: creates a column vector index from subsequent columns of array valArray on each iteration. For example, on the first iteration, index = valArray(:,1). The loop executes for a maximum of n times, where n is the number of columns of valArray, given by numel(valArray, 1, :). The input valArray can be of any MATLAB data type, including a string, cell array, or struct.

Therefore, I assume there is a significant overhead and the compiler does not check whether 1:ksize == klist to exploit the faster implementation. In other words, per Eitan's comment, the JIT applies to the first two types of accepted values.

The whole problem is related to the following indexing task (column vs element):

tic
for m = 1:100000        
    for k = 1:ksize
        klist(:,k);
    end        
end
toc

tic
for m = 1:100000        
    for k = 1:ksize
        klist(k);
    end        
end
toc

Index column:  ~2.9 sec
Index element: ~0.28 sec

You can see how klist(:,k) effectively slows down the faster loop indicating that the issue in for k = klist is related to the column indexing used in this case.

For additional details see this lengthy discussion on (inefficient) indexing.

2 of 2
2

My answer is speculation (because only Mathworks guys know the implementation of their product), but I think the first k loop is optimized to not create the actual array of indices, but to just scan them one by one, because it explicitely shows how the values are "built". The second k loop cannot be optimized, because the interpreter doesn't know beforehand if the content of the index array will grow uniformly. So, each time the loop starts, it will copy access the original klist and that's why you have the performance penalty.

Later edit: Another performance penalty might be from indexed access int the klist array, compared to creating the index values "on the fly."

๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 445512-indexing-an-entire-for-loop
indexing an entire for loop - MATLAB Answers - MATLAB Central
February 18, 2019 - Does the function call to x and y still for it's values or to k and l? Or do k and l only specify the array index to save the iterated value? If I were to adapt this to a 3d matrix, would I only need to declare the 3rd dimension of that matrix? Like, if there was a 3rd term using 'z' variable, it would be like this: ... Yes k,l and n do the actual indexing, we do this because the indices in MATLAB has to start from 1 (not less than or equal to zero).
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 26644-can-this-be-done-without-a-for-loop
can this be done without a for loop? - MATLAB Answers - MATLAB Central
January 20, 2012 - I already have implemented something similar to your loop solution. But since I figured out a way of setting the -1's without a loop, I was wondering if I could do everything without a loop. Sign in to comment. ... https://www.mathworks.com/matlabcentral/answers/26644-can-this-be-done-without-a-for-loop#answer_34747
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 375648-how-to-apply-a-function-to-an-indexed-array-without-loop
How to apply a function to an indexed array without loop - MATLAB Answers - MATLAB Central
January 4, 2018 - Is there a way to do the following A = rand(100,2); c = randi(5,[100,1]); for i = 1:5 B(i, :) = mean(A(c == i, :)); end without a loop? A is my input data, c is some...
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 12317-for-loop-index
for loop index - MATLAB Answers - MATLAB Central
July 25, 2011 - No, there is no provided mechanism for that. The loop index value is held in internal memory, and the statement is executed as if the loop variable is initialized to the current value at the top of the loop.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 141112-for-loop-not-working
For Loop Not Working?? - MATLAB Answers - MATLAB Central
July 9, 2014 - The for index variable in your case is TSR, there's no i involved and, "Yes, Virginia, you do not artificially index the for loop variable inside a for construct." doc for % and all will be explained...read all the doc including the _Tips_ section at the bottom. Then try the following exercise at the command line -- ... NB: while often used as "traditional" loop indices, in Matlab i and j are predefined as the imaginary sqrt(-1).
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 325556-set-of-indexes-to-vector-without-loop
Set of indexes to vector without loop? - MATLAB Answers - MATLAB Central
February 17, 2017 - would be one way to do it without a loop, if you consider arrayfun not to be a loop. It's more likely to be slower than an actual for loop. See also my old cody problem which asks to do just the same. For information, the best scoring solution is: ... whose only merit is that it is low scoring on cody. Certainly don't use that for real code, it's going to be very slow. Sign in to comment. ... https://www.mathworks.com/matlabcentral/answers/325556-set-of-indexes-to-vector-without-loop#answer_255190