insert zero into the column in the array
Mostrar comentarios más antiguos
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
Más respuestas (1)
Turbulence Analysis
el 5 de Feb. de 2022
0 votos
1 comentario
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});
disp(new_Data{1});
disp(Data{end});
disp(new_Data{end});
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
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!