Borrar filtros
Borrar filtros

Insert and write row with condition

5 visualizaciones (últimos 30 días)
Ravi Kumar
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
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
Ravi Kumar
Ravi Kumar el 25 de Oct. de 2021
I am not able to write a code.
what i want to do is this:
  1. insert a row above whenever the number changes in column 5
  2. update the row with values from column 1 and 2 and write the corresponding number in column 5
My column 3 and 4 are updated velocities and column 1 and 2 are old positions:
to get new positions I need the first position and then add the new velocities (my time step is 1)
let me know if more infor is needed

Iniciar sesión para comentar.

Respuesta aceptada

DGM
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).']
A = 16×5
628.0936 771.1265 0.2925 0.5543 1.0000 363.9163 406.2075 0.8559 0.9397 1.0000 787.0387 105.8189 0.4092 0.1386 1.0000 534.9079 486.2891 0.4776 0.2659 1.0000 369.6107 843.8638 0.8265 0.5216 2.0000 904.4099 909.2921 0.5373 0.8257 2.0000 1.4660 580.1854 0.0914 0.2954 2.0000 936.4754 960.9257 0.9290 0.7824 2.0000 336.0677 327.9915 0.5877 0.3315 3.0000 785.9644 533.4045 0.6154 0.8422 3.0000
% 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
B = 20×5
0 0 628.0936 771.1265 1.0000 628.0936 771.1265 0.2925 0.5543 1.0000 363.9163 406.2075 0.8559 0.9397 1.0000 787.0387 105.8189 0.4092 0.1386 1.0000 534.9079 486.2891 0.4776 0.2659 1.0000 0 0 369.6107 843.8638 2.0000 369.6107 843.8638 0.8265 0.5216 2.0000 904.4099 909.2921 0.5373 0.8257 2.0000 1.4660 580.1854 0.0914 0.2954 2.0000 936.4754 960.9257 0.9290 0.7824 2.0000
  2 comentarios
Ravi Kumar
Ravi Kumar el 26 de Oct. de 2021
Thanks alot.
Can you help me with one more?
I need to add element in column 1/2 and column 3/5 when the integer changes in column 5.
The new matrix should look the image below
DGM
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).']
A = 16×5
622.7034 64.4436 0.0389 0.1680 1.0000 740.4536 427.3152 0.0677 0.1048 1.0000 15.9007 214.7368 0.2870 0.5151 1.0000 686.3266 582.0770 0.3552 0.4706 1.0000 697.8795 308.9264 0.0877 0.0703 2.0000 993.1702 277.1825 0.0831 0.1208 2.0000 123.9105 262.3267 0.7042 0.5782 2.0000 291.2877 757.4814 0.3932 0.5392 2.0000 437.2965 10.3207 0.6664 0.8484 3.0000 51.1470 388.0229 0.2770 0.9828 3.0000
% 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)
B = 16×5
622.7034 64.4436 622.7423 64.6115 1.0000 740.4536 427.3152 0.0677 0.1048 1.0000 15.9007 214.7368 0.2870 0.5151 1.0000 686.3266 582.0770 0.3552 0.4706 1.0000 697.8795 308.9264 697.9672 308.9967 2.0000 993.1702 277.1825 0.0831 0.1208 2.0000 123.9105 262.3267 0.7042 0.5782 2.0000 291.2877 757.4814 0.3932 0.5392 2.0000 437.2965 10.3207 437.9629 11.1690 3.0000 51.1470 388.0229 0.2770 0.9828 3.0000

Iniciar sesión para comentar.

Más respuestas (0)

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!

Translated by