Find in a cell array? (2024)

1,386 views (last 30 days)

Show older comments

M G on 7 Aug 2013

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array

Commented: Marwan Malaeb on 20 May 2022

Accepted Answer: Jan

Hello all,

Suppose a cell array 10x1 consisted of random numbers from 1 to 5. How can I find the locations for number 5?

All the best,

MhD

3 Comments

Show 1 older commentHide 1 older comment

Jan on 7 Aug 2013

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163363

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163363

I've deleted the duplicate question.

Elias Berra on 17 Nov 2015

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_323672

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_323672

X = my_array_data [row,col] = find(X==21) %In this example, it retrieves the cell location which contains the value 21.

Marwan Malaeb on 20 May 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_2168535

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_2168535

call this array for example X

type k=find(X==5)

it will return for you the number of the cell that has the value of 5.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Jan on 7 Aug 2013

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#answer_93817

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#answer_93817

Edited: Jan on 7 Aug 2013

Open in MATLAB Online

C = {1,5,3,4,2,3,4,5,2,1};

index = find([C{:}] == 5);

Here [C{:}] is a faster inlined version of cell2mat.

Alternative:

index = cellfun(@(x) x==5, C, 'UniformOutput', 1);

Or the long and most likely faster form:

index = false(1, numel(C))

for k = 1:numel(C)

index(k) = (C{k} == 5);

end

[EDITED] If you are talking of a cell string, this is much faster:

D = {'1' '5' '3' '4' '2' '3' '4' '5' '2' '1'};

index = find(strcmp(D, '5'));

5 Comments

Show 3 older commentsHide 3 older comments

M G on 7 Aug 2013

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163378

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163378

Hey Jan,

Nice help. I just do not understand the difference between following two: C=[1] [5] [3] [4] [2] [3] [4] [5] [2] [1] ... which is what you said in your example and:

D='1' '5' '3' '4' '2' '3' '4' '5' '2' '1'

which I have the problem with. Both of them are cell arrays.However, your suggested way unfortunately doesn't work with "D".

Any idea to help me understand is appreciated :)

All the best....

Jan on 7 Aug 2013

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163385

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163385

Please do not write only "does not work", but explain what happens instead: Do you get an error message or does the result differ from your expectations?

It would be useful, if you post the real data directly to the question, because then the other users do not waste your and their time with not matching suggestions. Not that "D='1' '5'..." is not valid Matlab syntax, because the surrounding braces are essential.

While I expect my code to work with D fluently, when you search for the character '5' instead of the number 5. But Matlab offers a much faster solution then, see [EDITED]

M G on 8 Aug 2013

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163550

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163550

Open in MATLAB Online

strcmp works! Thanks. Here is data:

https://dl.dropboxusercontent.com/u/19202474/eheader.zip

Here is how it worked based on what you suggested.

S = char(cellfun(@(x) num2str(x),eheader,'Un',0))

Index = [];

j=1;

for i=1:size(S,1)

if(strcmp(S(i,:),' -88 ')==1)

Index(j) = i;

j = j+1;

end

end

Jan on 8 Aug 2013

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163570

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163570

Open in MATLAB Online

This looks strange. When eheader is numerical, converting it elementwise inside a cellfun call to a string and afterwards by char to a char matrix is cruel. Then assuming a certain number of spaces around the value is fragile, because the width depends on the values. What about this (I cannot open the posted MAT file, better post code in the forum which creates the example data):

x = find([eheader{:}] == -88)

STRCMP works much faster with cell strings, so at least do not let CHAR() create a CHAR-matrix.

Kylie Hansen on 16 Aug 2017

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_477364

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_477364

Just a casual MATLAB coder dropping by this older thread on a hunt for answers. Your response for the cell string method worked easily for me. Thank you so much for including it!

Sign in to comment.

More Answers (2)

Bill Tubbs on 15 Feb 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#answer_896925

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#answer_896925

Open in MATLAB Online

Just in case someone comes here looking to do this with a cell array of chars as I was, it's quite easy this way:

my_cell_array = {'a', 'b', 'c'};

i = find(strcmp(my_cell_array, 'b'));

assert(i == 2)

1 Comment

Show -1 older commentsHide -1 older comments

hongyi xu on 17 Apr 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_2107575

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_2107575

Genius! Your supplement exactly fits my question.

Sign in to comment.

Caroline on 7 Aug 2013

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#answer_93816

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#answer_93816

Edited: Azzi Abdelmalek on 7 Aug 2013

Open in MATLAB Online

cellarray_new = zeros; %initializing the array

ind = 1; %indices for new array

for j = 1:10

if (cellarray(j) == 5)

cellarray_new(ind) = j;

ind = ind + 1;

end

end

the array cellarray_new will contain all the indices of the original cell array that contain the number 5

3 Comments

Show 1 older commentHide 1 older comment

Azzi Abdelmalek on 7 Aug 2013

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163360

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163360

Why initializing cellarray_new to 0?

Jan on 7 Aug 2013

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163362

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_163362

I assume that "cell array" implies, that the array is a cell.

Filza Ashraf on 22 May 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_215393

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array#comment_215393

how can i find a pixel intensity if cell contains an image or image is stored in cell???

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsData TypesCharacters and Strings

Find more on Characters and Strings in Help Center and File Exchange

Tags

  • cell arrays
  • find

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Find in a cell array? (17)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Find in a cell array? (2024)

References

Top Articles
Hallmark Elevates Holiday Traditions with Seasonal Gifts and New Greeting Cards - Hallmark Corporate
Milana Grieco - Permanent Make-Up Hautinstitut : Kosmetikstudio in Rheine
Encore Atlanta Cheer Competition
Bild Poster Ikea
Craigslist St. Paul
Asist Liberty
Gamevault Agent
Ymca Sammamish Class Schedule
Mikayla Campinos Videos: A Deep Dive Into The Rising Star
Aries Auhsd
U.S. Nuclear Weapons Complex: Y-12 and Oak Ridge National Laboratory…
Slmd Skincare Appointment
No Hard Feelings Showtimes Near Cinemark At Harlingen
Games Like Mythic Manor
Grasons Estate Sales Tucson
Char-Em Isd
Roster Resource Orioles
Craighead County Sheriff's Department
Unterwegs im autonomen Freightliner Cascadia: Finger weg, jetzt fahre ich!
Craigslist Missoula Atv
97226 Zip Code
Tyler Sis University City
Euro Style Scrub Caps
Tips on How to Make Dutch Friends & Cultural Norms
Pecos Valley Sunland Park Menu
Spn 520211
Yisd Home Access Center
3569 Vineyard Ave NE, Grand Rapids, MI 49525 - MLS 24048144 - Coldwell Banker
Villano Antillano Desnuda
Radical Red Ability Pill
Truck from Finland, used truck for sale from Finland
The Clapping Song Lyrics by Belle Stars
Weather Underground Durham
Solo Player Level 2K23
Top Songs On Octane 2022
Ripsi Terzian Instagram
Hellgirl000
Infinite Campus Farmingdale
Noaa Duluth Mn
Craigslist en Santa Cruz, California: Tu Guía Definitiva para Comprar, Vender e Intercambiar - First Republic Craigslist
All Obituaries | Sneath Strilchuk Funeral Services | Funeral Home Roblin Dauphin Ste Rose McCreary MB
Craigslist Odessa Midland Texas
Saline Inmate Roster
Valls family wants to build a hotel near Versailles Restaurant
Gander Mountain Mastercard Login
Steam Input Per Game Setting
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Makemkv Key April 2023
Lira Galore Age, Wikipedia, Height, Husband, Boyfriend, Family, Biography, Net Worth
Festival Gas Rewards Log In
Psalm 46 New International Version
Kobe Express Bayside Lakes Photos
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6151

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.