Ho to keep entries in a for loop
이전 댓글 표시
Hello hi i have a question how do i keep the entries in a for loop
답변 (1개)
Marc Jakobi
2016년 10월 4일
You are pre-initializing ele with a zero, that's why it gets stored in ASCII in every loop iteration. I would change your code to the following:
nbr_elem = input('Enter the total number of elements in the molecule: ');
ele = [];
occ = zeros(1, nbr_elem) %it is good practise to pre-initialize arrays with their full size
for x = 1:nbr_elem
ele = [ele, input('Enter an element: ','s')];
occ(x) = input('Enter the element''s occurrence in the molecule: ');
end
Also, I would suggest using
while ~strcmp(ele(x), table_periodique(j(x),2))
in your while loop instead of ~= There is also no need to index j(x), you can just set j = 1. Hope this helps!
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!