Why do the values not get added into the array such as [2,3,5,7], but instead they get summed up?

1 vue (au cours des 30 derniers jours)
clear;
close all
clc;
N = 10;
list=2:N;
values = [2];
list(find(mod(list,2)==0))=[];
tester = 3;
while ~isempty(list)
list(find(mod(list,tester)==0))=[];
tester = list(1);
values = values + [, tester];
end
  3 commentaires
Image Analyst
Image Analyst le 2 Déc 2019
I just copied and pasted your code from above and this is what it says:
Index exceeds the number of array elements (0).
Error in test (line 11)
tester = list(1);
That's because after a few iterations, list is null. If you want, you can try it yourself. But anyway, glad you got an answer.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 2 Déc 2019
Modifié(e) : Matt J le 2 Déc 2019
Because you are using '+'. To concatenate, you would do,
values = [values, tester];

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by