Filling missing elements in a matrix.
4 views (last 30 days)
Show older comments
Hello,
I have a matrix like this:
A = [1 0 2 0 0 2 2 2 0 2 2 2 [] [] [] [] [] ;
0 3 2 1 0 1 2 0 1 0 0 0 [] [] [] [] [] ;
0 2 3 0 2 0 1 3 0 0 0 0 [] [] [] [] [] ;
0 0 2 2 0 1 1 0 2 0 0 0 [] [] [] [] [] ;
3 0 3 0 0 0 0 1 0 1 0 1 [] [] [] [] [] ;
4 0 4 0 0 1 1 0 0 0 0 1 0 [] [] [] [] ;
0 0 3 0 0 0 0 0 0 0 0 0 0 [] [] [] [] ;
0 0 2 3 0 0 0 0 0 0 0 0 0 2 [] [] [] ;
0 0 0 3 0 1 0 0 0 0 0 [] 1 0 [] [] [] ;
4 0 0 0 3 3 1 0 0 [] 0 [] 0 0 [] 0 0 ;
0 0 0 0 2 5 1 0 0 0 0 0 0 0 [] 0 0 ;
0 0 0 0 0 1 0 0 0 0 0 0 0 0 [] 0 0 ;
0 0 2 [] 1 3 0 0 0 0 0 0 0 0 [] 0 0 ;
2 2 2 3 0 0 0 0 0 0 0 0 0 0 [] 0 0 ;
0 1 1 2 1 0 [] 0 [] 0 [] 0 [] 0 [] 0 0 ;
0 0 0 [] 0 0 0 [] 0 [] 0 [] [] 0 [] 0 0 ;
1 0 1 [] 0 0 0 0 [] 0 [] 0 [] 0 [] 0 0 ;
0 1 0 0 0 0 0 [] 0 [] 0 [] [] 0 [] 0 0 ;
[] 3 0 1 0 0 [] 0 [] 0 [] 0 0 0 [] 0 [] ;
[] [] [] [] 0 0 0 [] 0 [] 0 [] 0 0 [] 0 [] ;
[] [] [] [] 0 [] [] [] [] [] [] [] [] [] [] [] [] ;
[] [] [] 0 [] 0 0 0 [] 0 [] 0 0 0 0 [] [] ;
[] [] [] [] [] 0 0 [] 0 [] 0 [] 0 0 0 [] [] ;
[] [] [] [] 0 0 [] 0 [] 0 [] 0 0 0 [] [] [] ];
The [] denote missing elements. I want to fill in all the missing elements by holding the previous value in the same row (value to the left of the missing element) and in cases where the row begins with [] it should fill in the values from the right, again in the same row.
Please help me, I've tried the fillmissing function but it does not produce the results I am looking for.
Thanks in advance.
1 Comment
Adam Danz
on 3 Dec 2019
Edited: Adam Danz
on 3 Dec 2019
That doesn't describe your real variable 'A'. In your description, A is a matrix but matrices cannot have empty values. They can have NaN values, but not empty values.
I'm guessing A is actually a cell array of scalar values that looks like this (execute this in the command window and please confirm this assumption).
A = num2cell(randi(9,20,10)); % random integers
A(randi(numel(A),1,100)) = {[]}; % remove 100 values and replace with empty
Also, what if the preceeding value (or the next value) is also empty?
Accepted Answer
Adam Danz
on 3 Dec 2019
Edited: Adam Danz
on 3 Dec 2019
Here's a demo
% Create a 20x10 cell array of integer values
% and replace 100 of the values with empties
A = num2cell(randi(9,20,10));
A(randi(numel(A),1,100)) = {[]};
% Fill empties with NaNs
A(cellfun(@isempty,A)) = {NaN};
% Convert to matrix and replace all empties with
% previous value except rows that lead with empty
F = fillmissing(cell2mat(A).','previous').';
% Replace the leftover rows the lead with empties
F = fillmissing(F.','next').';
Alternatively you could try this but in my quick tests, it didn't work so well
F = fillmissing(cell2mat(A).','previous','EndValues','next').';
If you need to convert the matrix back to a cell array (not recommended)
C = num2cell(F);
2 Comments
More Answers (0)
See Also
Categories
Find more on Performance and Memory 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!