Replacing empty cells using for loop

20 次查看(过去 30 天)
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)

采纳的回答

dpb
dpb 2021-1-22
编辑:dpb 2021-1-23
Short answer is, "you can't".
A double array cannot have an empty element; it is either empty or full.
A cell array is the only way in which I see you can do this other than, perhaps you could cobble something together with sparse maybe???
The only alternative I see in an ordinary array would be to replace the empty cell with NaN or some other indicator value.
ses1_results{cellfun(@isempty,ses1_results)}=nan;
res=cell2mat(ses1_results);
res(res==0)=[];
returns
res =
1.1920
0.5678
1.0000
NaN
1.0000
>>
Or, of course, following the comment; you've already got a cell array; just remove the cells that are zero.
>> ses1_results(~cellfun(@(v)isequal(v,0),ses1_results))
ans =
5×1 cell array
{[ 1.1920]}
{[ 0.5678]}
{[ 1]}
{0×0 double}
{[ 1]}
>>

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-1-22
ses1_results = {1.192;0;0;0;0.5678;0;0;0;0;0;0;1;[];1}
ses1_results = 14x1 cell array
{[ 1.1920]} {[ 0]} {[ 0]} {[ 0]} {[ 0.5678]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 1]} {0×0 double} {[ 1]}
mask = cellfun(@(C) numel(C) == 1 && C == 0, ses1_results);
rest = ses1_results(~mask)
rest = 5x1 cell array
{[ 1.1920]} {[ 0.5678]} {[ 1]} {0×0 double} {[ 1]}
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}
ses1_results = 14x1 cell array
{[ 1.1920]} {[ 0]} {[ 0]} {[ 0]} {[ 0.5678]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 1]} {0×0 double} {[ 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)
RxTa = 14×1
1.1920 999.0000 999.0000 999.0000 0.5678 999.0000 999.0000 999.0000 999.0000 999.0000
  2 个评论
Owen Gray
Owen Gray 2021-1-22
Thanks for the reply, I tried to give this a go but I realised I had made a typo, instead of 1s its empty 0x0 cells, so like this
ses1_results = 14x1 cell array
{[ 1.1920]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0.5678]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 0]}
{[ 1]}
{0×0 double}
{[ 1]}
I tried your code but substituting numel (C) for isempty but got this error:
Operands to the || and && operators must be convertible to logical scalar values.
Walter Roberson
Walter Roberson 2021-1-22
? 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}
ses1_results = 14x1 cell array
{[ 1.1920]} {[ 0]} {[ 0]} {[ 0]} {[ 0.5678]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 1]} {0×0 double} {[ 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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by