How to store the cell array elements into logical array?

2 views (last 30 days)
mpr=zeros(nodes,nodes);
for i=1:nodes
mprset=QMPR{i};
for j=1:nodes
if (ismember(mprset,j))
mpr(i,j)=1;
end
end
end
the logical array values not updated please help me..
  1 Comment
Stephen23
Stephen23 on 6 Nov 2015
Edited: Stephen23 on 6 Nov 2015
Can you please show us an example element/cell of QMPR.
And please tell us exactly what you want as an output. Showing broken code does not explain things, but when you tell us what you want to do then we can help you because we understand what you need.

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 6 Nov 2015
Edited: Thorsten on 6 Nov 2015
As Stephen and Guillaume pointed out:
To compare if single element j is member of the set mprset, use
if ismember(j,mprset)
The statement you use
ismember(mprset, j)
returns an array the size of mprset, where all the elements that equal j are set to 1, else 0. And an array is only true if all elements are 1.

More Answers (2)

Stephen23
Stephen23 on 6 Nov 2015
Edited: Stephen23 on 6 Nov 2015
The answer depends on information that you have not given us. For example how many elements mprset has, and what values those arrays have. If there are multiple values in those arrays, then the
if ismember(mprset,j)
operation will only run the code inside that statement when all of the elements of mprset are equal to j. If any one element is not equal to j, then that if statement will not run.
The if documentation states that "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false." So any value not equal to j will make the whole expression false.
Without knowing what QMPR and your aims are we are limited in how we can help you. For a start, please tell us exactly what you want to get at the output, and the exact conditions or calculations involved. Given broken code does not really help us, because it does not tell us what you want to do.

Guillaume
Guillaume on 6 Nov 2015
As per Stephen's comment, a example cell from QMPR would help greatly.
I strongly suspect that your if line is not working as intended. Is there a missing function (like any maybe) preceding the unnecessary bracket before ismember?
I assume mprset is a vector or matrix (if it's scalar, it's pointless to store it a cell array), therefore ismember will return a logical array. if, when passed a logical array, executes the statement only if all the elements are true. So the statement is equivalent to:
if all(ismember(mprset, j)) %and if that's what was intended, then the all should be explicit
That if is only true if mprset is just a vector / matrix of j.
Maybe you meant
if any(ismember(mprset, j))
%or you can simply reverse the arguments to get a scalar logical
if ismember(j, mprset) %no unnecessary brackets either
which will be true if mprset contains j.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!