How to find the exact values in a column and make a computation according to the next column of it?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi; I have matrix that first column shows task numbers, second column shows the station numbers that tasks assigned and third column shows the machine types assigned to regarding stations..
For example:
A=
1 1 1
5 1 2
4 2 2
2 2 2
3 3 2
7 3 1
6 4 2
9 4 1
this matrix says that task 1 and task 5 assigned to workstation1 and machice type 1 and machine type 2 is neccesarry.
task 4 and task 2 assigned to workstation2 and only machine 2 is neccesarry and only 1 machine2 is neccasary (two tasks are assigned to one machine). Now I want to compute the machine numbers for the stations that :
mac=
2
1
2
2
Thanks in advance;
Regards..
댓글 수: 0
채택된 답변
Guillaume
2016년 2월 22일
편집: Guillaume
2016년 2월 22일
It's not very clear what you're asking. If all you want is to know how many different machines are required for each station, it's simply:
A = [1 1 1
5 1 2
4 2 2
2 2 2
3 3 2
7 3 1
6 4 2
9 4 1];
mac = accumarray(A(:, 2), A(:, 3), [], @(x) numel(unique(x)))
accumarray groups the machines by station and the anonymous function @(x) numel(unique(x)) counts the number of unique machine for each station.
Note that the code will only work correctly if the stations are consecutive integers starting at 1. If not:
[station, ~, subs] = unique(A(:, 2));
mac = accumarray(subs, A(:, 3), [], @(x) numel(unique(x)))
will work regardless of the station values.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!