Replacing empty cells using for loop
Mostrar comentarios más antiguos
Hi there, I have 796x1 cell array consisting of 1x1 scalar arrays, or empty arrays. E.g.
>> ses1_results = {1.192;0;0;0;0.5678;0;0;0;0;0;0;1;[];1}
ses1_results =
14×1 cell array
{[ 1.1920]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0.5678]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 1]}
{0×0 double}
{[ 1]}
The spacing of the 0s is irregular
I need to remove all the cells with 0, whilst maintaining the 0x0 cells and the values>0, and then convert that into an array. How can I do this? I've spent a few hours trying to do it like this but can't seem to figure it out:
Rxtcell = ses1_results(5:800,6);
for n = 1:796
Z = []
Q = 0
x = Rxtcell(n)
if 1 == strcmp(x,Q)
Rxtcell{n}=[999];
elseif 1== strcmp(x,Z)
Rxtcell(n)= [0];
else
end
end
RxTa = cell2mat(Rxtcell)
Respuesta aceptada
Más respuestas (1)
ses1_results = {1.192;0;0;0;0.5678;0;0;0;0;0;0;1;[];1}
mask = cellfun(@(C) numel(C) == 1 && C == 0, ses1_results);
rest = ses1_results(~mask)
However, your code suggests you want to do something different than that.
ses1_results = {1.192;0;0;0;0.5678;0;0;0;0;0;0;1;[];1}
mask0 = cellfun(@(C) numel(C) == 1 && C == 0, ses1_results);
maske = cellfun(@isempty, ses1_results);
Rtxcell = ses1_results;
Rtxcell(mask0) = {999};
Rtxcell(maske) = {0};
RxTa = cell2mat(Rtxcell)
2 comentarios
Owen Gray
el 22 de En. de 2021
? I do not see any difference between what you posted in that comment and what you posted earlier?
ses1_results = {1.192;0;0;0;0.5678;0;0;0;0;0;0;1;[];1}
Note that numel(C) == 1 is not testing the content of an entry for value 1: it is testing whether there is exactly one entry in the cell. You see, you did not promise that each entry was either empty or had exactly one value in it. Your attempt to turn the cell into an array will fail if any cells have more than 1 entry in them, but that's a problem for later.
If you can promise that each entry is either empty or contains exactly one entry then you can replace
mask0 = cellfun(@(C) numel(C) == 1 && C == 0, ses1_results);
with
mask0 = cellfun(@(C) ~isempty(C) && C == 0, ses1_results);
which will fail during the cellfun() phase if there are any entries that have more than 1 value in them.
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!