insert zero into the column in the array

3 visualizaciones (últimos 30 días)
Turbulence Analysis
Turbulence Analysis el 5 de Feb. de 2022
Comentada: Voss el 5 de Feb. de 2022
Hi,
I have a array of size 49 x 3 (Attached here), here, I would like to make entries in the thrird column starts from 1 to 100. Next, for each missed entries in the third column, i need to insert zero in the second column. Please let men know how to do this ..

Respuesta aceptada

Voss
Voss el 5 de Feb. de 2022
Here is my best guess at what you're asking for:
load('matlab.mat');
new_A = zeros(100,3);
new_A(:,1) = 2;
new_A(:,3) = 1:100;
[ism,idx] = ismember(1:100,A(:,3));
new_A(ism,2) = A(idx(ism),2);
disp(new_A);
2.0000 0 1.0000 2.0000 0 2.0000 2.0000 498.7000 3.0000 2.0000 0 4.0000 2.0000 0 5.0000 2.0000 0 6.0000 2.0000 0 7.0000 2.0000 457.1000 8.0000 2.0000 0 9.0000 2.0000 333.6000 10.0000 2.0000 0 11.0000 2.0000 445.3000 12.0000 2.0000 0 13.0000 2.0000 465.6000 14.0000 2.0000 0 15.0000 2.0000 356.3000 16.0000 2.0000 395.8000 17.0000 2.0000 816.4000 18.0000 2.0000 398.2000 19.0000 2.0000 0 20.0000 2.0000 0 21.0000 2.0000 434.1000 22.0000 2.0000 0 23.0000 2.0000 0 24.0000 2.0000 0 25.0000 2.0000 568.7000 26.0000 2.0000 0 27.0000 2.0000 370.2000 28.0000 2.0000 503.6000 29.0000 2.0000 424.1000 30.0000 2.0000 404.1000 31.0000 2.0000 0 32.0000 2.0000 0 33.0000 2.0000 384.7000 34.0000 2.0000 586.6000 35.0000 2.0000 0 36.0000 2.0000 437.4000 37.0000 2.0000 0 38.0000 2.0000 0 39.0000 2.0000 0 40.0000 2.0000 526.7000 41.0000 2.0000 0 42.0000 2.0000 0 43.0000 2.0000 0 44.0000 2.0000 469.4000 45.0000 2.0000 0 46.0000 2.0000 381.2000 47.0000 2.0000 0 48.0000 2.0000 0 49.0000 2.0000 481.0000 50.0000 2.0000 477.8000 51.0000 2.0000 450.8000 52.0000 2.0000 0 53.0000 2.0000 514.1000 54.0000 2.0000 0 55.0000 2.0000 517.4000 56.0000 2.0000 409.6000 57.0000 2.0000 581.6000 58.0000 2.0000 436.5000 59.0000 2.0000 0 60.0000 2.0000 0 61.0000 2.0000 0 62.0000 2.0000 518.5000 63.0000 2.0000 0 64.0000 2.0000 0 65.0000 2.0000 510.8000 66.0000 2.0000 539.7000 67.0000 2.0000 439.2000 68.0000 2.0000 536.2000 69.0000 2.0000 0 70.0000 2.0000 644.8000 71.0000 2.0000 354.5000 72.0000 2.0000 383.5000 73.0000 2.0000 0 74.0000 2.0000 542.0000 75.0000 2.0000 0 76.0000 2.0000 0 77.0000 2.0000 0 78.0000 2.0000 0 79.0000 2.0000 662.4000 80.0000 2.0000 422.9000 81.0000 2.0000 487.4000 82.0000 2.0000 379.7000 83.0000 2.0000 0 84.0000 2.0000 0 85.0000 2.0000 0 86.0000 2.0000 0 87.0000 2.0000 0 88.0000 2.0000 0 89.0000 2.0000 0 90.0000 2.0000 0 91.0000 2.0000 0 92.0000 2.0000 442.5000 93.0000 2.0000 516.6000 94.0000 2.0000 0 95.0000 2.0000 453.7000 96.0000 2.0000 427.6000 97.0000 2.0000 767.7000 98.0000 2.0000 496.5000 99.0000 2.0000 480.8000 100.0000
  5 comentarios
Turbulence Analysis
Turbulence Analysis el 5 de Feb. de 2022
I just performed undo and accpted you answer.. Hope its okay now... Sorry for th inconvenience.
Voss
Voss el 5 de Feb. de 2022
Perfect! I appreciate it!

Iniciar sesión para comentar.

Más respuestas (1)

Turbulence Analysis
Turbulence Analysis el 5 de Feb. de 2022
Hi,
Thanks a lot.. This is what exactly I looking for..
May I know how to implement this, for multiple such arrays located inside the cell.., for example in the attached cell named Data {}
  1 comentario
Voss
Voss el 5 de Feb. de 2022
Sure. Make those few lines of code into a function and then call the function on each cell using cellfun().
(I modified the code to keep the values in the first column the same as what they were in the original matrix, rather than setting them all to 2, since they vary in the matrices in this cell array.)
load('matlab.mat');
new_Data = cellfun(@fill_to_100,Data,'UniformOutput',false);
disp(Data{1});
0 440.8000 7.0000 0 291.6000 8.0000 0 282.8000 10.0000 0 387.8000 12.0000 0 362.8000 14.0000 0 523.4000 17.0000 0 520.7000 18.0000 0 292.2000 22.0000 0 382.9000 28.0000 0 287.5000 31.0000 0 387.7000 34.0000 0 634.2000 35.0000 0 443.5000 37.0000 0 442.9000 45.0000 0 405.6000 46.0000 0 387.6000 47.0000 0 315.0000 50.0000 0 363.8000 51.0000 0 594.0000 54.0000 0 655.2000 56.0000 0 559.8000 58.0000 0 506.2000 63.0000 0 455.4000 66.0000 0 637.3000 67.0000 0 320.8000 68.0000 0 651.9000 69.0000 0 555.4000 71.0000 0 511.0000 75.0000 0 409.6000 77.0000 0 331.6000 81.0000 0 512.4000 82.0000 0 360.6000 83.0000 0 426.5000 84.0000 0 451.2000 94.0000 0 519.3000 96.0000 0 381.7000 97.0000 0 522.8000 99.0000 0 451.6000 100.0000
disp(new_Data{1});
0 0 1.0000 0 0 2.0000 0 0 3.0000 0 0 4.0000 0 0 5.0000 0 0 6.0000 0 440.8000 7.0000 0 291.6000 8.0000 0 0 9.0000 0 282.8000 10.0000 0 0 11.0000 0 387.8000 12.0000 0 0 13.0000 0 362.8000 14.0000 0 0 15.0000 0 0 16.0000 0 523.4000 17.0000 0 520.7000 18.0000 0 0 19.0000 0 0 20.0000 0 0 21.0000 0 292.2000 22.0000 0 0 23.0000 0 0 24.0000 0 0 25.0000 0 0 26.0000 0 0 27.0000 0 382.9000 28.0000 0 0 29.0000 0 0 30.0000 0 287.5000 31.0000 0 0 32.0000 0 0 33.0000 0 387.7000 34.0000 0 634.2000 35.0000 0 0 36.0000 0 443.5000 37.0000 0 0 38.0000 0 0 39.0000 0 0 40.0000 0 0 41.0000 0 0 42.0000 0 0 43.0000 0 0 44.0000 0 442.9000 45.0000 0 405.6000 46.0000 0 387.6000 47.0000 0 0 48.0000 0 0 49.0000 0 315.0000 50.0000 0 363.8000 51.0000 0 0 52.0000 0 0 53.0000 0 594.0000 54.0000 0 0 55.0000 0 655.2000 56.0000 0 0 57.0000 0 559.8000 58.0000 0 0 59.0000 0 0 60.0000 0 0 61.0000 0 0 62.0000 0 506.2000 63.0000 0 0 64.0000 0 0 65.0000 0 455.4000 66.0000 0 637.3000 67.0000 0 320.8000 68.0000 0 651.9000 69.0000 0 0 70.0000 0 555.4000 71.0000 0 0 72.0000 0 0 73.0000 0 0 74.0000 0 511.0000 75.0000 0 0 76.0000 0 409.6000 77.0000 0 0 78.0000 0 0 79.0000 0 0 80.0000 0 331.6000 81.0000 0 512.4000 82.0000 0 360.6000 83.0000 0 426.5000 84.0000 0 0 85.0000 0 0 86.0000 0 0 87.0000 0 0 88.0000 0 0 89.0000 0 0 90.0000 0 0 91.0000 0 0 92.0000 0 0 93.0000 0 451.2000 94.0000 0 0 95.0000 0 519.3000 96.0000 0 381.7000 97.0000 0 0 98.0000 0 522.8000 99.0000 0 451.6000 100.0000
disp(Data{end});
7.0000 332.6000 1.0000 7.0000 301.1000 2.0000 7.0000 302.9000 4.0000 7.0000 282.4000 5.0000 7.0000 317.4000 6.0000 7.0000 313.4000 7.0000 7.0000 302.0000 8.0000 7.0000 283.9000 11.0000 7.0000 341.0000 12.0000 7.0000 341.5000 13.0000 7.0000 307.2000 16.0000 7.0000 282.7000 17.0000 7.0000 283.8000 18.0000 7.0000 336.4000 19.0000 7.0000 278.6000 20.0000 7.0000 297.2000 21.0000 7.0000 290.2000 22.0000 7.0000 300.3000 24.0000 7.0000 345.2000 26.0000 7.0000 297.0000 27.0000 7.0000 314.4000 28.0000 7.0000 288.6000 29.0000 7.0000 279.4000 31.0000 7.0000 373.6000 32.0000 7.0000 303.2000 33.0000 7.0000 311.4000 34.0000 7.0000 293.3000 36.0000 7.0000 321.8000 37.0000 7.0000 280.3000 38.0000 7.0000 314.2000 41.0000 7.0000 280.1000 42.0000 7.0000 299.4000 43.0000 7.0000 296.4000 44.0000 7.0000 288.5000 47.0000 7.0000 358.5000 48.0000 7.0000 291.0000 49.0000 7.0000 274.6000 50.0000 7.0000 297.0000 51.0000 7.0000 346.8000 53.0000 7.0000 286.6000 54.0000 7.0000 297.1000 55.0000 7.0000 352.7000 56.0000 7.0000 303.2000 58.0000 7.0000 302.0000 59.0000 7.0000 281.6000 60.0000 7.0000 290.1000 61.0000 7.0000 352.2000 63.0000 7.0000 323.6000 64.0000 7.0000 290.7000 65.0000 7.0000 273.1000 66.0000 7.0000 314.8000 69.0000 7.0000 328.4000 72.0000 7.0000 386.3000 73.0000 7.0000 302.3000 74.0000 7.0000 313.3000 75.0000 7.0000 313.9000 76.0000 7.0000 317.2000 77.0000 7.0000 323.5000 78.0000 7.0000 296.9000 79.0000 7.0000 358.4000 81.0000 7.0000 295.8000 82.0000 7.0000 314.0000 84.0000 7.0000 340.9000 85.0000 7.0000 325.0000 86.0000 7.0000 319.1000 87.0000 7.0000 313.2000 90.0000 7.0000 299.2000 91.0000 7.0000 341.7000 92.0000 7.0000 274.9000 93.0000 7.0000 319.1000 94.0000 7.0000 318.7000 95.0000 7.0000 287.2000 96.0000 7.0000 347.6000 97.0000 7.0000 301.6000 99.0000
disp(new_Data{end});
7.0000 332.6000 1.0000 7.0000 301.1000 2.0000 7.0000 0 3.0000 7.0000 302.9000 4.0000 7.0000 282.4000 5.0000 7.0000 317.4000 6.0000 7.0000 313.4000 7.0000 7.0000 302.0000 8.0000 7.0000 0 9.0000 7.0000 0 10.0000 7.0000 283.9000 11.0000 7.0000 341.0000 12.0000 7.0000 341.5000 13.0000 7.0000 0 14.0000 7.0000 0 15.0000 7.0000 307.2000 16.0000 7.0000 282.7000 17.0000 7.0000 283.8000 18.0000 7.0000 336.4000 19.0000 7.0000 278.6000 20.0000 7.0000 297.2000 21.0000 7.0000 290.2000 22.0000 7.0000 0 23.0000 7.0000 300.3000 24.0000 7.0000 0 25.0000 7.0000 345.2000 26.0000 7.0000 297.0000 27.0000 7.0000 314.4000 28.0000 7.0000 288.6000 29.0000 7.0000 0 30.0000 7.0000 279.4000 31.0000 7.0000 373.6000 32.0000 7.0000 303.2000 33.0000 7.0000 311.4000 34.0000 7.0000 0 35.0000 7.0000 293.3000 36.0000 7.0000 321.8000 37.0000 7.0000 280.3000 38.0000 7.0000 0 39.0000 7.0000 0 40.0000 7.0000 314.2000 41.0000 7.0000 280.1000 42.0000 7.0000 299.4000 43.0000 7.0000 296.4000 44.0000 7.0000 0 45.0000 7.0000 0 46.0000 7.0000 288.5000 47.0000 7.0000 358.5000 48.0000 7.0000 291.0000 49.0000 7.0000 274.6000 50.0000 7.0000 297.0000 51.0000 7.0000 0 52.0000 7.0000 346.8000 53.0000 7.0000 286.6000 54.0000 7.0000 297.1000 55.0000 7.0000 352.7000 56.0000 7.0000 0 57.0000 7.0000 303.2000 58.0000 7.0000 302.0000 59.0000 7.0000 281.6000 60.0000 7.0000 290.1000 61.0000 7.0000 0 62.0000 7.0000 352.2000 63.0000 7.0000 323.6000 64.0000 7.0000 290.7000 65.0000 7.0000 273.1000 66.0000 7.0000 0 67.0000 7.0000 0 68.0000 7.0000 314.8000 69.0000 7.0000 0 70.0000 7.0000 0 71.0000 7.0000 328.4000 72.0000 7.0000 386.3000 73.0000 7.0000 302.3000 74.0000 7.0000 313.3000 75.0000 7.0000 313.9000 76.0000 7.0000 317.2000 77.0000 7.0000 323.5000 78.0000 7.0000 296.9000 79.0000 7.0000 0 80.0000 7.0000 358.4000 81.0000 7.0000 295.8000 82.0000 7.0000 0 83.0000 7.0000 314.0000 84.0000 7.0000 340.9000 85.0000 7.0000 325.0000 86.0000 7.0000 319.1000 87.0000 7.0000 0 88.0000 7.0000 0 89.0000 7.0000 313.2000 90.0000 7.0000 299.2000 91.0000 7.0000 341.7000 92.0000 7.0000 274.9000 93.0000 7.0000 319.1000 94.0000 7.0000 318.7000 95.0000 7.0000 287.2000 96.0000 7.0000 347.6000 97.0000 7.0000 0 98.0000 7.0000 301.6000 99.0000 7.0000 0 100.0000
function new_A = fill_to_100(A)
new_A = zeros(100,3);
new_A(:,1) = A(1,1);
new_A(:,3) = 1:100;
[ism,idx] = ismember(1:100,A(:,3));
new_A(ism,2) = A(idx(ism),2);
end

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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