How to find conditional unique value in matrix
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Helle everyone,
I have a dataset;
x = [100 10 1; 100 30 2 ; 100 40 1; 100 75 1 ; 101 10 2; 101 25 2 ; 101 50 1 ; 102 20 1; 102 25 2; 103 50 1]
the first column indicates order number,
the second column indicates cost,
the third column indicates firm. There are 2 firms and I want to find maximum value of order cost with respect to firms. If there is cost for that firm, I want to fill value as 0.
So, I want to find 2 different matrices.
for the firm 1;
firm1 = [100 75 1;101 50 1; 102 20 1; 103 50 1]
and firm2
firm2 = [100 30 2; 101 50 2; 102 20 2; 103 0 2]
note that for order number 103 cost value is 0 because there is no information for firm 2 about this order.
I want to use unique command with a loop but it fails.
Thank you so much in advance.
Regards,
0 commentaires
Réponses (2)
Spencer Chen
le 3 Fév 2020
Maybe this will get you started:
ux1 = unique(x(:,1)); % Find your unique order numbers
for xx = 1:numel(ux1)
idx = x(:,1) == ux1(xx) & x(:,3) == 1; % Find all orders from firm 1 with the desired order number
...
end
Blessings,
Spencer
0 commentaires
Voir également
Catégories
En savoir plus sur Downloads dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!