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

조회 수: 5 (최근 30일)
Carlos Alvarez-Santullano
Carlos Alvarez-Santullano 2017년 5월 5일
답변: ai ping Ng 2017년 5월 7일
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
Carlos Alvarez-Santullano 2017년 5월 5일
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
Carlos Alvarez-Santullano 2017년 5월 5일
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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by