How to write a loop that does not increase by +1?

19 ビュー (過去 30 日間)
Cside
Cside 2020 年 2 月 22 日
編集済み: John D'Errico 2020 年 2 月 22 日
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
  3 件のコメント
David Goodmanson
David Goodmanson 2020 年 2 月 22 日
Hi charms, you can do
for k = 2:2:8
(do stuff that depends on k)
end
since the middle 2 sets the increment value
Cside
Cside 2020 年 2 月 22 日
編集済み: Cside 2020 年 2 月 22 日
@madhan my expected result would just be [R,P], for the respective A input by the loops
@david - k is different with every experiment I am running. Would be great if there is a way to embedd it!

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2020 年 2 月 22 日
編集済み: John D'Errico 2020 年 2 月 22 日
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
  1 件のコメント
Cside
Cside 2020 年 2 月 22 日
I see, thank you! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by