How to find max value and divide it in two rows

2 views (last 30 days)
Xie
Xie on 6 Jun 2017
Commented: Xie on 6 Jun 2017
I have matrix X as follows:
X = [66 101 1050 1200
66 102 1200 1370
66 106 1370 1450
67 103 1050 1060
67 105 1060 1190
67 105 1190 1450
68 101 1000 1050
68 102 1050 1400
69 103 1200 1265
69 104 1265 1600];
I want to check the forth column in last row belong to the every different ID (e.g. row 3, 6, 8 & 10) and check if they are <=1400 or not. If they are larger than 1400 then the difference with 1400 should be calculated and a new row added to the matrix. In new row, first and second arrays from previous row should be repeated, and third array should be 1000 and forth array should be 1000+the difference found earlier.

Answers (1)

Andrei Bobrov
Andrei Bobrov on 6 Jun 2017
Edited: Andrei Bobrov on 6 Jun 2017
X = [66 101 1050 1200
66 102 1200 1370
66 106 1370 1450
67 103 1050 1060
67 105 1060 1190
67 105 1190 1450
68 101 1000 1050
68 102 1050 1400
69 103 1200 1265
69 104 1265 1600];
t0 = accumarray(findgroups(X(:,1)),1);
ii = cumsum(t0);
t = X(ii,4) > 1400;
a = t0+t;
iend = cumsum(a);
idx = true(iend(end),1);
idx(iend(t)) = false;
out = zeros(iend(end),size(X,2));
out(idx,:) = X;
jj = find(~idx);
out(jj,:) = out(jj-1,:);
out(jj,3) = 1000;
out(jj,4) = out(jj,4) - 1400 + 1000;

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!