insert zero into the column in the array
Afficher commentaires plus anciens
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 ..
Réponse acceptée
Plus de réponses (1)
Turbulence Analysis
le 5 Fév 2022
0 votes
1 commentaire
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
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!