How to compare two logical arrays

조회 수: 35 (최근 30일)
Rhiannon Jones
Rhiannon Jones 2019년 6월 6일
댓글: Rhiannon Jones 2019년 6월 6일
Hi,
I would like to compare two logical cell arrays, creating a third logical cell array returning true for when index values of both logical cell arrays are true.
My error;
Undefined unary operator '~' for input arguments of type 'cell'.
Is there a way to do this for cell?
Many thanks
% the two different logical arrays produced by cellfun:
fun_stf_p = cellfun(@(x) x >= stf_p_lb & x <= stf_p_ub, PRES, 'UniformOutput', false);
fun_stf_s = cellfun(@(x) x >= stf_s_lb & x <= stf_s_ub, PSAL, 'UniformOutput', false);
% Create a third logical cell array for which both cell arrays return true
fun_stf = ~fun_stf_p & ~fun_stf_s; % doesn't work
  댓글 수: 2
Adam
Adam 2019년 6월 6일
편집: Adam 2019년 6월 6일
Why do you have logical cell arrays in the first place? If they have to be the same length (which they do for you to compare them element by element) and they are both logical then why not just put them in a logical array and use
c = a & b;
type of logic? Surely you don't need the 'UniformOutput', 'false' if the two arrays fulfil those criteria.
Rhiannon Jones
Rhiannon Jones 2019년 6월 6일
Both cell arrays are a series of double arrays which match in dimension between the cell arrays, but change size within the cell array.

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

채택된 답변

Matthew Sisson
Matthew Sisson 2019년 6월 6일
편집: Matthew Sisson 2019년 6월 6일
One option is to continue using cellfun for this, as follows:
fun_stf = cellfun(@(x,y) ~x & ~y,fun_stf_p,fun_stf_s)
Another option is to convert to a double and then back again. Is there a reason why you are using cell arrays in the first place? If vectors are all the same length, working with logical arrays will be easier & faster.
  댓글 수: 2
Rhiannon Jones
Rhiannon Jones 2019년 6월 6일
Thanks for your answer! Both cell arrays are a series of double arrays which match in dimension between the cell arrays, but change size within the cell array.
This code returns logical true for when both cells are 0 or 1. I only want it to return logical true when they both satisfy the conditions and return logical true. Sorry if that wasn't clear.
Rhiannon Jones
Rhiannon Jones 2019년 6월 6일
I'm guessing this would be x==1 & y==1 instead of ~x & ~y, which seems to be working

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by