Insert and write row with condition
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ravi Kumar
el 25 de Oct. de 2021
Comentada: DGM
el 27 de Oct. de 2021
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 comentarios
Mathieu NOE
el 25 de Oct. de 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
Respuesta aceptada
DGM
el 26 de Oct. de 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 comentarios
DGM
el 27 de Oct. de 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)
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!