Effacer les filtres
Effacer les filtres

how to find the quantity of the object based on the given data by using if-else loop?

1 vue (au cours des 30 derniers jours)
hello i want to find the number of the each fruit based on the given data as below by using if-else loop can any body help me with that thank you
Data = apple orange apple grape grape apple banana grape grape orange apple apple apple grape grape
For assisting the development of the program, program is initiated by defining those fruits as listed below:
A = apple O = orange G = grape B = banana
  1 commentaire
Jan
Jan le 15 Juin 2015
Modifié(e) : Jan le 15 Juin 2015
Please post the input data in valid Matlab syntax, because a pseudo-code like this line demands for some bold guessing:
Data = apple orange apple grape grape apple banana grape grape orange apple apple apple grape grape
There is no "if-else loop" neither in Matlab nor in any other programming language I know. The IF command simply does not start a loop.
The question looks like a homework. Then it is more useful to post, what you have tried so far and ask a specific question.

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 15 Juin 2015
Defining 4 different variables to ddefined the categories is a bad idea. So the first thing you need is to combine them in one cell array:
A = 'apple';
O = 'orange';
G = 'grape';
B = 'banana';
Fruit = {A, O, G, B};
Data = {'apple', 'orange', 'apple', 'grape', 'grape', 'apple', 'banana', ...
'grape', 'grape', 'orange', 'apple', 'apple', 'apple', 'grape', 'grape'};
Now the ismember command and histc would solve the problem efficiently. But I assume that you are asked for a loop and an if-else branching. So start with creating a loop over the elements of Data and use strcmp to find out to which category each string belongs.
  1 commentaire
amina shafanejad
amina shafanejad le 15 Juin 2015
how should i use the strcmp command for this loop?can u explain to me please? ><

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Object Programming 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