If loop to know if event happens on a matrix then 1, how to do it?

2 次查看(过去 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
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.
Carlos Alvarez-Santullano
Im sorry for my english. The thing is i have a matrix where the first column is the code of the item sold and the second column is the week where that item were sold. Im trying to know the frecuency of sales of each item im working with (meaning this if the item has been sold every week or just in a few weeks).
This is my matrix
Item week
0012 1
0013 1
0012 2
0012 2
0012 2
0013 2
0013 3
n 53
So i want to know how to make a code that give me as a result this
Item week1 week2 week3 week4 week5
0012 1 1 0 0 1
0013 0 0 1 0 0
n 1 0 1 1 0
Meaning this the item 0012 were sold on week1 week2 week5 no sales for week3 and week4.
And it would be so much better if possible if the code result is this:
Item week1 week2 week3 week4 week5
0012 1 2 0 0 3
0013 0 0 1 2 0
n 1 0 3 2 0
Meaning this the item 0012 were sold once on week1 twice on week2 and 3 times on week 5.

请先登录,再进行评论。

回答(3 个)

Andrei Bobrov
Andrei Bobrov 2017-5-5
iw = [13 1
14 1
42 2
52 3
63 3];
any(ismember(iw,[13, 1],'rows'))

Guillaume
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 个评论
Carlos Alvarez-Santullano
thanks man! everything is ok but the
t = array2table([item, itemperweek], 'VariableNames', [{'Item'}; cellstr(compose('week%d', week))])
do you know whats wrong with it? because im working with a lot of data and the pretty display would be great :)
Guillaume
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
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

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by