6 views (last 30 days)

Hello :) I currently need to run a loop with the output of "find(collated(1,:) ==0 )". Currently, the output is [2 ; 4 ; 5 ; 6 ; 8], and I need to run the loop with these indexes. I couldn't use a for loop as the number are not increasing by 1. Is there a way I could manage the loop so that the output does not have to run in an +1 order? I read that a while loop may be useful, but am still new to the while loop.

PS

the matrix 'collated' is a 35 x 9 logical, so the idea that I want the code to work is that with each row of collated, I find the 0s i.e in row 1, it will be [2 ; 4 ; 5 ; 6 ; 8], row 2 [2 ;3], row 3 [4; 7; 8; 9] etc...

Appreciate any sort of help! :)

for k = 1:35

while A == find(collated(k,:) ==0)

selectedlocal = find(sesslocal == A);

pfc = dataset_p(1,:);

fef = dataset_f(1,:);

zpfc = zscore(pfc);

zfef = zscore(fef);

zzpfc = zpfc(selectedlocal);

zzfef = zfef(selectedlocal);

[R,P] = corrcoef(zzpfc,zzfef)

end

end

John D'Errico
on 22 Feb 2020

Edited: John D'Errico
on 22 Feb 2020

A for loop can have any increment. Even a vector of numbers can be the increment. And it need not be in any order. Some examples:

% standard unit increment

for n = 1:10

stuff

end

% increment of 2

for n = 2:2:12

stuff

end

% only the prime numbers, that do not exceed 100

P = primes(100);

for n = P

stuff

end

% an arbitrary set of numbers

nvals = [1 10 100 1000 10000 100000];

for n = nvals

stuff

end

% an arbitrary set of numbers that need not be increasing, or even be integers

% the values might even be complex numbers.

nvals = [2 1 5 14 pi -1000 1 3 1.273435345 2+3i];

for n = nvals

disp(n)

end

2

1

5

14

3.14159265358979

-1000

1

3

1.273435345

2 + 3i

% the row vector input to for need not be a double precision vector.

% It may even be a cell vector

nvals = {1 pi,'bcde'};

for n = nvals

disp(n)

end

[1]

[3.14159265358979]

'bcde'

% or a character vector.

nvals = 'The quick brown fox jumped over the lazy dog'

for n = nvals

disp(n)

end

T

h

e

q

u

i

c

k

b

r

o

w

n

f

o

x

j

u

m

p

e

d

o

v

e

r

t

h

e

l

a

z

y

d

o

g

In a few cases, I've reported the value of n at each step through the loop. As you can see, the elements of the row vector may be any number. It can be of class uint8 or double, or even a cell vector.

Be careful of one thing however. The for statement operates as you would expect ONLY when given a ROW vector. It iterates across the COLUMNS of the input. So if the argument to for is a COLUMN vector, then the for loop runs for only one step. If it is a matrix, then the result will be a sequence of vectors!

% A 3x3 matrix, so there will be THREE iterations.

% n will be each of the columns of nvals in turn.

nvals = magic(3)

nvals =

8 1 6

3 5 7

4 9 2

for n = nvals

disp('A column')

disp(n)

end

A column

8

3

4

A column

1

5

9

A column

6

7

2

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506940-how-to-write-a-loop-that-does-not-increase-by-1#comment_800737

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506940-how-to-write-a-loop-that-does-not-increase-by-1#comment_800737

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506940-how-to-write-a-loop-that-does-not-increase-by-1#comment_800740

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506940-how-to-write-a-loop-that-does-not-increase-by-1#comment_800740

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506940-how-to-write-a-loop-that-does-not-increase-by-1#comment_800751

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/506940-how-to-write-a-loop-that-does-not-increase-by-1#comment_800751

Sign in to comment.