How do I remove the brackets from the numeric values around the zeros, so that only the numeric values are left and I am able to use boolean logic to get a solution ( 1 or 0)?
6 次查看(过去 30 天)
显示 更早的评论
"( (1) | (0) )"
"(0)"
"( (0) | (1) | (0) )"
"( (0) | (0) )"
"( (1) | (0) )"
""
"(0)"
"( (0) & (1) & (0) )"
"(1)"
"( (0) | (0) | (0) )"
""
"(( (0) & (0) & (1) & (0) & (0) & (1) & (0) & (0) ) | ( (0) & (0) & (0) & (1) & (0) & (0) & (1) & (0) & (0) ))"
""
3 个评论
Daniel M
2019-11-14
I agree. The format is strange. I doubt you really have a comma separated list of strings like that.
采纳的回答
Daniel M
2019-11-14
编辑:Daniel M
2019-11-14
Hmm, seems like this was an opportunity to apply what you learned yesterday regarding regexprep, if you're trying to remove/replace strings. But you likely don't have to. It also seems that these already are boolean expressions in the form of strings. For example
x = ["( (1) | (0) )", "(0)", "( (0) | (1) | (0) )", "( (0) | (0) )", "( (1) | (0) )", "", "(0)", "( (0) & (1) & (0) )", "(1)", "( (0) | (0) | (0) )", "", "(( (0) & (0) & (1) & (0) & (0) & (1) & (0) & (0) ) | ( (0) & (0) & (0) & (1) & (0) & (0) & (1) & (0) & (0) ))", ""];
boolexp = arrayfun(@str2num, x, 'UniformOutput',0);
ans =
1×13 cell array
Columns 1 through 10
{[1]} {[0]} {[1]} {[0]} {[1]} {0×0 double} {[0]} {[0]} {[1]} {[0]}
Columns 11 through 13
{0×0 double} {[0]} {0×0 double}
Note that it returns empty where the string is empty. You can deal with this with
emptyInds = cellfun(@(v) isempty(v), boolexp, 'UniformOutput',1);
boolexp(emptyInds) = {NaN};
Unfortunately, can't use cell2mat to convert to an array because boolexp contains logicals and NaN. You would have to convert the logicals to doubles first (but then it wouldn't be boolean, would it?). Or deal with the empty cells another way. Or leave it the way it is. Up to you.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!