MATLAB Answers

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)?

조회 수: 3(최근 30일)
Michael Tross
Michael Tross 14 Nov 2019
댓글: Michael Tross 15 Nov 2019
"( (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

the cyclist
the cyclist 14 Nov 2019
Can you upload a file with actual data in it, that is storing your input? That will be much more efficient than us asking you questions about what you really mean. (For example, are those string arrays? Or a cell array that has strings? Or a cell array that has characters arrays? Etc.)
A small example of the input, and expect output (even if you cannot write the algorithm that takes you from one to the other) is very helpful.
Daniel M
Daniel M 14 Nov 2019
I agree. The format is strange. I doubt you really have a comma separated list of strings like that.
Michael Tross
Michael Tross 15 Nov 2019
Thank you! I was able to get the solutions for the expressions using 'arrayfun' below. If there are anymore "burning" questions, I'll be sure to upload the file.

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel M
Daniel M 14 Nov 2019
편집: Daniel M 14 Nov 2019
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개)

Community Treasure Hunt

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

Start Hunting!

Translated by