フィルターのクリア

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

1 回表示 (過去 30 日間)
amina shafanejad
amina shafanejad 2015 年 6 月 15 日
コメント済み: amina shafanejad 2015 年 6 月 15 日
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 件のコメント
Jan
Jan 2015 年 6 月 15 日
編集済み: Jan 2015 年 6 月 15 日
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.

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2015 年 6 月 15 日
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 件のコメント
amina shafanejad
amina shafanejad 2015 年 6 月 15 日
how should i use the strcmp command for this loop?can u explain to me please? ><

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by