Insert and write row with condition
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Mathieu NOE
am 25 Okt. 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
Akzeptierte Antwort
DGM
am 26 Okt. 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 Kommentare
DGM
am 27 Okt. 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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!