If loop to know if event happens on a matrix then 1, how to do it?
1 回表示 (過去 30 日間)
古いコメントを表示
Im working with a really big matrix of sales and i want to know if theres a sale of each item on a week (already separate them)
for example matrix:
item code week
[13 1
14 1
42 2
52 3
63 3]
i want the answer to be 1 if there was a sale of the item 13 on week 1
2 件のコメント
Image Analyst
2017 年 5 月 5 日
Sale is ambiguous in English. Do you mean sale, like "at least one item was sold", or do you mean sale like "the price is now discounted as compared to the usual price"?
For the first case, we'd need the number sold during that week. So one additional column for the number sold would be needed.
For the second case, we'd need to know the price during the week, and the usual (non-sale) price, so two additional columns are needed.
回答 (3 件)
Andrei Bobrov
2017 年 5 月 5 日
iw = [13 1
14 1
42 2
52 3
63 3];
any(ismember(iw,[13, 1],'rows'))
0 件のコメント
Guillaume
2017 年 5 月 5 日
m = [0012 1
0013 1
0012 2
0012 2
0012 2
0013 2
0013 3];
[item, ~, destrow] = unique(m(:, 1));
[week, ~, destcol] = unique(m(:, 2));
itemperweek = accumarray([destrow, destcol], 1);
%for pretty display:
t = array2table([item, itemperweek], 'VariableNames', [{'Item'}; cellstr(compose('week%d', week))])
2 件のコメント
Guillaume
2017 年 5 月 6 日
Works fine for me. What is the error you're getting? And which version of matlab are you using? compose requires at least R2016b.
If you're using an earlier version (upgrade!) then you can use the undocumented sprintfc instead:
t = array2table([item, itemperweek], 'VariableNames', [{'Item'}; sprintfc('week%d', week)])
ai ping Ng
2017 年 5 月 7 日
I am sorry for interrupting here, but can anyone help with my question? I am currently searching and changing many methods for matrix in for loop but seems doesn't help https://www.mathworks.com/matlabcentral/answers/339045-how-can-i-select-the-rows-in-matrix-using-for-loop
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!