Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:58:28+00:00 2026-05-27T04:58:28+00:00

I am relatively new to Matlab and have a question about creating a logical

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:58:28+00:00Added an answer on May 27, 2026 at 4:58 am

    If your input is a cell array (we’ll call it data), then I typically use cellfun for this sort of work.

    C = cellfun(@(x) ~isempty(x) && ischar(x) && strcmp(x,'U') , data(:,2))
    

    This defines an anonymous function which returns true for 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 of data, and returns it in an array C.

    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.

     C = logical(zeros(size(data,1),1));  %Always preallocate your results vector
     for ix = 1:size(data,1)
         curValue = data{ix,2}l
         C(ix) =  ~isempty(curValue ) && ischar(curValue ) && strcmp(curValue ,'U');
     end
    

    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:

     for ix = 1:size(data,1)
         curValue = data{ix,2};
         data{ix,3} =  ~isempty(curValue) && ischar(curValue) && strcmp(curValue ,'U');
     end
    

    EDIT

    If your data is in a cell array of arrays (based on a recent comment), then try:

    C = mydata{2} == 'U';
    

    This extracts the character array, and compares each element to the character 'U'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being relatively new to the software industry I have come across a question of
Relatively new to Cocoa here. This question is about NSFileHandle, but I got a
I'm relatively new to Java EE and have already began to hear about the
I am relatively new to web development and web applications. I have heard about
I'm relatively new to J2ME and about to begin my first serious project. My
I'm relatively new to web application programming so I hope this question isn't too
I'm relatively new to Nant, what i'd like to do is have a task
Im relatively new to PHP but have realized it is a powerfull tool. So
I am a relatively new programmer so this may be a really simple question
I'm relatively new to Haskell. I'm creating a small api/dsl on top of happstack-lite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.