Insert and write row with condition
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Mathieu NOE
il 25 Ott 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
Risposta accettata
DGM
il 26 Ott 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 Commenti
DGM
il 27 Ott 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)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!