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 个)
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!