User inputed vector - Index exceeds matrix dimensions.

I can't seem to get this code to have the user input the vector.
error message: Index exceeds matrix dimensions.
code below:
e = zeros(1,5);
cnt = 0;
while 1==1
cnt = cnt+1;
if (cnt == 6)
break;
e(cnt) = input('Elements: ')
end
end
e = e(1:cnt);
w=[16 12 14 32 1];
disp('molecular weight: ')
w*e

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 28일
편집: Azzi Abdelmalek 2012년 11월 28일

0 개 추천

Because when cnt==6; you can't calculate e = e(1:cnt); (numel(e)=5)
Image Analyst
Image Analyst 2012년 11월 28일
Try doing it this way:
userString = char(inputdlg('Enter the numbers'))
theNumbers = sscanf(userString, '%f')
Jan
Jan 2012년 11월 29일
e = zeros(1,5);
for cnt = 1:5
e(cnt) = input('Elements: ')
end

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

Jon
2012년 11월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by