Insert and write row with condition
4 views (last 30 days)
Show older comments
I have a large matrix 248201*5 (see the first image below). I need to put values from coulmn 1/2 to column 3/4, with condition in coulmn 5.
The new matrix should look this this. I have done 3 iteration manually. I need to do it for the whole matrix.
2 Comments
Mathieu NOE
on 25 Oct 2021
hello
sorry but the logic is not super clear for me
would better if you could describe like in a pseudo code what the scenario in column 5 will do on the data
Accepted Answer
DGM
on 26 Oct 2021
I'm sure there are other ways, but here's one example.
% create test array
nb = 4;
sb = 4;
A = [rand(nb*sb,2)*1000 rand(nb*sb,2) repelem(1:nb,sb).']
% index lists
tol = 0.5; % abs change in col 5 must exceed this value
bpin = [1; find(abs(diff(A(:,5)))>tol)+1];
bpout = bpin + (0:numel(bpin)-1).';
% convert output indices to a mask
mask = false(size(A,1)+numel(bpin),1);
mask(bpout) = true;
% construct output
B = zeros(size(A,1)+numel(bpin),size(A,2));
B(mask,3:5) = A(bpin,[1 2 5]);
B(~mask,:) = A
2 Comments
DGM
on 27 Oct 2021
Not knowing what exactly this second example started as, I'm going to assume this works:
% create test array
nb = 4;
sb = 4;
A = [rand(nb*sb,2)*1000 rand(nb*sb,2) repelem(1:nb,sb).']
% index lists
tol = 0.5;
bpin = [1; find(abs(diff(A(:,5)))>tol)+1];
% construct output
B = A;
B(bpin,3:4) = B(bpin,1:2) + B(bpin,3:4)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!