Counting every other number in a list
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nathaniel Wolff
am 12 Sep. 2020
Kommentiert: the cyclist
am 12 Sep. 2020
Howdy.
I have a matrix
mylist = [1:10]
I need to count every other number in this matrix
I can't use the odd or even distinguishing tests such as...
for i = 1:length(mylist)
if rem(mylist(i),2) ~= 0;
eo_total = eo_total + 1
end
end
Because this only counts the odd numbers in my for loop. So if I had a matrix
mylist(2) = [ 2 4 6 8 10]
eo_total = 0 instead of 2
any ideas?
0 Kommentare
Akzeptierte Antwort
the cyclist
am 12 Sep. 2020
I'm not sure what you mean by "count" them, but the following vector will store every other element from mylist, starting with the first one:
out = mylist(1:2:end)
2 Kommentare
the cyclist
am 12 Sep. 2020
Oh. Then it's probably more efficient to do
ceil(numel(mylist)/2)
Then you don't need to create a second vector.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!