I am relatively new to Matlab and have a question about creating a logical array in Matlab
Suppose I have the following data. A contains actual values and B column denotes U as undetected
A B
2 U
4 U
5
6 U
6
7 U
8
I would like to create a logical array such as undetected values get a 1 and else 0. Thus the c column would look some thing like this:
C = 1 1 0 1 0 1 0
Thank you for your help.
If your input is a cell array (we’ll call it
data), then I typically usecellfunfor this sort of work.This defines an anonymous function which returns
truefor any input which is (1) not empty, (2) a character array and (3) has a value of ‘U’ and. It calls that function on each element in the second column ofdata, and returns it in an arrayC.I like that it fits a single operation on a single line. It is easy to read after you use the construction enough times (but perhaps not easy to read the first few times).
A more typical (and perhaps easier to understand) solution is to use a loop.
Finally, it sounds like you may actually want your result back in your original cell array, in the third column. That is a small change to the loop above, like so:
EDIT
If your data is in a cell array of arrays (based on a recent comment), then try:
This extracts the character array, and compares each element to the character
'U'.