How to find conditional unique value in matrix

4 次查看(过去 30 天)
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,

回答(2 个)

Spencer Chen
Spencer Chen 2020-2-3
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

Berk Gulmez
Berk Gulmez 2020-2-4
Thanks for comment,
with using unique command, I found required values.
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];
%finding indices of first firm
xr = find(x(:,3)==1);
%subset of first firm
xrr = x(xr,:);
%finding indices of second firm
xl = find(x(:,3)==2);
%subset of second firm
xll = x(xl,:);
%finding the biggest cost value with respect to order for the first firm
[~,I]=unique(xrr(:,1),'last');
xRMD = xrr(I,:);
%finding the biggest cost value with respect to order for the first firm
[~,I]=unique(xll(:,1),'last');
xLMD = xll(I,:);
%finding order number
ux1=unique(x(:,1));
now I need to find a result like that but ı cannot use ismember function correctly,
result = [100 75 30; 101 50 25; 102 20 25; 103 50 0]
can you please give an advice?
Thank you so much in advance.

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by