If loop to know if event happens on a matrix then 1, how to do it?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Image Analyst
am 5 Mai 2017
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.
Antworten (3)
Andrei Bobrov
am 5 Mai 2017
iw = [13 1
14 1
42 2
52 3
63 3];
any(ismember(iw,[13, 1],'rows'))
0 Kommentare
Guillaume
am 5 Mai 2017
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 Kommentare
Guillaume
am 6 Mai 2017
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
am 7 Mai 2017
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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!