insert zero into the column in the array
Show older comments
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 ..
Accepted Answer
More Answers (1)
Turbulence Analysis
on 5 Feb 2022
0 votes
1 Comment
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
Categories
Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!