Filling missing elements in a matrix.

5 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2019-12-3
编辑:Adam Danz 2019-12-3
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?

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-12-3
编辑:Adam Danz 2019-12-3
Use F = fillmissing(A,method); requires >=r2016b
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by