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 750285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:32:00+00:00 2026-05-14T14:32:00+00:00

I have an array of empty cells and ones that I want to convert

  • 0

I have an array of empty cells and ones that I want to convert to a logical array, where the empty cells are zeros. When I use cell2mat, the empty cells are ignored, and I end up with a matrix of solely 1’s, with no reference to the previous index they held. Is there a way to perform this operation without using loops?

Example code:

for n=1:5              %generate sample cell array
    mycellarray{n}=1;
end
mycellarray{2}=[]      %remove one value for testing

Things I’ve tried:

mylogicalarray=logical(cell2mat(mycellarray));

which results in [1,1,1,1], not [1,0,1,1,1].

for n=1:length(mycellarray)
    if isempty(mycellarray{n})
       mycellarray{n}=0;
    end
end
mylogicalarray=logical(cell2mat(mycellarray));

which works, but uses loops.

  • 1 1 Answer
  • 1 View
  • 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-14T14:32:01+00:00Added an answer on May 14, 2026 at 2:32 pm

    If you know your cell array is only going to contain ones and [] (which represent your zeroes), you can just use the function cellfun to get a logical index of the empty cells, then negate the index vector:

    mylogicalarray = ~cellfun(@isempty, mycellarray);
    % Or the faster option (see comments)...
    mylogicalarray = ~cellfun('isempty', mycellarray);
    

    If your cells could still contain zero values (not just []), you could replace the empty cells with 0 by first using the function cellfun to find an index for the empty cells:

    emptyIndex = cellfun('isempty', mycellarray);     % Find indices of empty cells
    mycellarray(emptyIndex) = {0};                    % Fill empty cells with 0
    mylogicalarray = logical(cell2mat(mycellarray));  % Convert the cell array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have an array of objects that is shared between two different
I have a cell array where some cells are empty, some are with numbers
I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now
I have an empty array into which I am pushing values using javascript. I
Have an array of chars like char members[255]. How can I empty it completely
I have created an empty json object having an array itemlist(which further contains itemid
I have an included javascript file thats initializes an empty array object called widgets
I have a gridview with empty cells. Whenever a cell is clicked I replace
I have an unidimensional array that can have more the 50 elements and I'd
I have a TableLayoutPanel control that is initially empty - 0 rows and 0

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.