フィルターのクリア

How to compare column values of two rows from a cell and report the third row(labes) according to the comparsion

3 ビュー (過去 30 日間)
How can I write the code of these two desired output for this problem?
The problem is that there are a Shopping List(items) which is a string array and a Price List which is numerical array, both with the same length since each price is assigned to an item. so for instance, we have:
ShoppingList = ["apple" , "orange" , "bread" , "milk" , "butter" , "crackers" , "carrots" , "zuchine" , "pasta" , "wine"];
PriceList = [12 25 10 20 1 10 15 18 5 11];
Then we want to set a value as a threshold(THR) that if any given price is higher than that, we decide to not buy it; so that then:
1) In our cell, we would have a new row with reported values of 0 or 1 for "not buy" or "buy" assigned to each item according to the price threshold
2) Extract a new string array in which all the reported items have a price less than threshold.
So far I coded in this way to build a cell with 3 rows for comparison and data exctraction ...
ShoppingList = ["apple" , "orange" , "bread" , "milk" , "butter" , "crackers" , "carrots" , "zuchine" , "pasta" , "wine"];
PriceList = [12 25 10 20 1 10 15 18 5 11];
prompt = "How much money do you want to spend on each item of the list?";
THR = input(prompt) % threshold setting
THR_array = repelem(THR, 10) % Make the thresold value into an array with the same length as the other two arrays
CELL = [num2cell(ShoppingList) ; num2cell(PriceList) ; num2cell(THR_array)] % a cell with 3 rows
But I don't know how to build that 0/1 vector as a new row of the cell accodring to the threshold as well as how I can report the items (from the first row) which has a price below the threshold ...
I appreciate a clear respone to have these two outputs. Also if there is any other way to get this output in a more simpler way, I would like to know how! Thank you!
  6 件のコメント
Tommy
Tommy 2020 年 5 月 4 日
DId you look into logical indexing? Discussed at the bottom of this page:
If I have a cell array, say
>> a = cellstr(('a':'j')')'
a =
1×10 cell array
{'a'} {'b'} {'c'} {'d'} {'e'} {'f'} {'g'} {'h'} {'i'} {'j'}
as well as the logical array from my first comment:
>> PriceList<10
ans =
1×10 logical array
0 0 0 0 1 0 0 0 1 0
then indexing into my cell array with the logical array gives the following output:
>> a(PriceList<10)
ans =
1×2 cell array
{'e'} {'i'}
Amin S
Amin S 2020 年 5 月 4 日
Ah I see... all I needed was only logical indexing! thank you! now it works!

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by