deleting numbers in cell array
5 次查看(过去 30 天)
显示 更早的评论
I have a 32x5 cell array.
I want to delete all numbers (+/- 3 either side of each number) that do not appear in every column.
Is there a function that can help me?
Thanks
Sam
6 个评论
Robert U
2020-2-25
The description of your problem is ambiguous. It is not clear how your algorithm should work neither the sequential order of commands is clear. I encourage you to describe the desired functionality step-by-step which might help you to write your own code quickly.
From what you wrote (but still not fitting your example values):
- Compare the first element of input matrix within its row with all other values.
- If all other values of said row are not within first element +/- 3, remove first element (by replacing it with NaN)
- Repeat steps 1 & 2 with all other values within input matrix.
- Move all remaining values (not NaN) as much rows up keeping the same column as possible without replacing or reordering numerical values.
Example:
dInput = [10 11 10 28 9; ...
39 40 41 40 39; ...
58 47 58 57 59; ...
68 58 68 69 70];
dInMin = dInput - 3;
dInMax = dInput + 3;
for numRow = 1:size(dInput,1)
for numCol = 1:size(dInput,2)
bRange = dInput(numRow,numCol) >= dInMin(numRow,:) & dInput(numRow,numCol) <= dInMax(numRow,:);
bRange(numCol) = 0;
if ~any(bRange)
dInput(numRow,numCol) = NaN;
end
end
end
dInput = [10 11 10 NaN 9
39 40 41 40 39
58 NaN 58 57 59
68 NaN 68 69 70]
Does it really make sense to move remaining values up?
Kind regards,
Robert
采纳的回答
Robert U
2020-3-2
Hi sam van Bohemen,
Thank you for your description. I guess the following code will do the trick:
dInput = [10 11 10 28 9; ...
39 40 41 40 39; ...
58 47 58 57 59; ...
68 58 68 69 70];
dInMin = dInput - 3;
dInMax = dInput + 3;
for numRow = 1:size(dInput,1)
for numCol = 1:size(dInput,2)
bRange = all(any(dInput(numRow,numCol) >= dInMin(:,[1:numCol-1,numCol+1:end]) & dInput(numRow,numCol) <= dInMax(:,[1:numCol-1,numCol+1:end]),1));
if ~bRange
dInput(numRow,numCol) = NaN;
end
end
end
bRowMoved = true;
while bRowMoved
for numRow = 2:size(dInput,1)
bRowMoved = false;
for numCol = 1:size(dInput,2)
if isnan(dInput(numRow-1,numCol))
dInput(numRow-1,numCol) = dInput(numRow,numCol);
dInput(numRow,numCol) = NaN;
bRowMoved = true;
end
end
end
dInput(all(isnan(dInput),2),:) = [];
end
Kind regards,
Robert
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!