Trouble Matching Data in Indexed Array
조회 수: 1 (최근 30일)
이전 댓글 표시
Hey, everyone! I'm trying to create a program where the Array "Accepted" has a direct correlation to the Array "Values". For example "A" would be given value "1", "B", "2", and so on. and then at the end it asks how many units of the variable of "Accepted" and divides it by its given value. So if I said 7 units of "C" it would return "7/3 total C". So i created these arrays but i don't know how i would link them up that way. the way I have it I feel that it would just take the entire array rather than assigning each array a specific value of the other. ideas?
thanks!
Accepted = {'A','B', 'C'};
Values = {'1','2','3'} ;
letter = input('what letter? ','s');
if any(strcmpi(elmnt,Accepted))
disp ('\n')
else
disp ('that''s not going to work')
end
units = input ('how many units of it?');
fprintf('%d units of (Accepted)', units/(Values))
댓글 수: 0
답변 (1개)
Iain
2013년 6월 10일
You can get the index of the matched item easily:
index = find(strcmpi(elmnt,Accepted));
I find disp, and concatenation easier to handle than fprintf
disp([num2str(units) '/' Values{index} ' of "' elmnt{index} '".'])
댓글 수: 5
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!