How to fill in missing rows in a Matrix
2 views (last 30 days)
Show older comments
Hey Guys,
i have a big matrix like so:
-1 0.5 0.0
-1 0.5 0.02
-1 0.5 0.04
...
-1 0.5 1.5
-1 0.45 0.0
-1 0.45 0.02
...
and so on.
In some rows the third column stops before it reaches 1.5.
Is there a way that i can detect where the numeration has stopped and in the second step, am I able to insert the missing Data?
For your aknowledgment, there are more columns after the third one, they could be empty oder filled with zeros.
Thanks in regards for your help!
2 Comments
Accepted Answer
Jan
on 22 Jan 2019
Edited: Jan
on 22 Jan 2019
% Create the full array manually:
v = (0:0.02:1.5).';
k = [0.5, 0.45].'; % I cannot guess which values you have here
M(:, 2:3) = [repelem(k, numel(v), 1), repmat(v, numel(k), 1)];
M(:, 1) = -1;
% Find matching rows and insert the full rows:
[match, index] = ismember(M, YourMatrix(:, 1:3), 'rows');
M(match, :) = YourMatrix(index(match), :);
0 Comments
More Answers (2)
Walter Roberson
on 22 Jan 2019
mask = diff(YourMatrix(:,2)).' < 0;
jump_idx = find(mask);
val_at_jump = YourMatrix(jump_idx, 2);
too_short = find(val_at_jump ~= 1.5);
where_to_insert = jump_idx(too_short);
0 Comments
See Also
Categories
Find more on Optimization 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!