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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:18:01+00:00 2026-06-17T13:18:01+00:00

Suppose I have a cell A = {[3,0], [2,1]} and a cell array B

  • 0

Suppose I have a cell

A = {[3,0], [2,1]}

and a cell array

B = {[4,-1],[3,0];      
      [-1,4],[-3,5];
      [3,0],[2,1];
      [2,1],[-1,4]}.

I want to find the indices where both the first or second entry in A shows up in B excluding the row in B where both entries of A show up.

In this example I should get something like [1 4] for the rows in B. I’ve been trying to figure this out using cellfun and cell2mat but keep stumbling.

  • 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-06-17T13:18:02+00:00Added an answer on June 17, 2026 at 1:18 pm

    I would approach this problem by converting my cell arrays to numeric arrays of appropriate dimensions, and then use ismember.

    The following example illustrates how this method works on the example cell arrays in the question:

    %# Build the example cell arrays
    A = {[3,0], [2,1]};
    B = {[4,-1],[3,0];      
         [-1,4],[-3,5];
         [3,0],[2,1];
         [3,0],[3,0];
         [2,1],[-1,4]};
    
    %# Get the number of elements in A, and the length of the first element
    K = size(A, 2);
    J = length(A{1, 1});
    
    %# Convert cell arrays to usefully shaped numerical matrices
    ANumVec = cell2mat(A);
    ANum = reshape(ANumVec, K, J)';
    BNum = cell2mat(B);
    
    %# Find matches of 1*2 vectors in ANum in sets of two columns of BNum 
    I1 = ismember(BNum(:, 1:J), ANum, 'rows');
    I2 = ismember(BNum(:, J+1:end), ANum, 'rows');
    I3 = ismember(BNum, ANumVec, 'rows');
    
    %# Find all indices where there was exactly 1 match (ie omit cases of no matches and cases of 2 matches)
    MainIndex = I1 + I2;
    MainIndex(I3) = 0;
    Soln = find(MainIndex > 0);
    

    Some points:

    1) This method finds the indices of all rows in B where an element of A lies in the first or second column of B, excluding the situation where A corresponds exactly to a row of B.

    2) This method will fail if there are multiple rows in A. However, it is robust to A being a cell array of size 1*N, where N denotes some arbitrary number of 1*2 numeric vectors. Thus the single row limitation can be circumvented by first reshaping A to a 1*N cell array.

    3) Equivalence is tested using the logical operator ==. This can be dangerous with floating point numbers unless you have reason to believe a priori that your inputs will not exhibit any floating point error.

    4) I can’t shake the feeling that there is a much more efficient way to solve this problem, but that I’m not seeing it at the moment. 🙂

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

Sidebar

Related Questions

Suppose I have a cell array with 3 elements; the first and third cells
Suppose you have a JTable and for each cell you want to display three
Suppose I have a cell array containing an array of integer arrays. What is
Suppose you have an NxN grid, and you want to visit every cell exactly
Suppose I have a cell like A = {'erogol' 'grerol' 'biral'} then I want
Suppose I have a table like so: <table> <tr><td class=this-is-a-label>Label Cell</td></tr> <tr><td>Detail 1</td></tr> <tr><td
Suppose that I have a method called doSomething() and I want to use this
Suppose i have the following Moose package: package GSM::Cell; use Moose; has 'ID' =>
Suppose I have the 2 cell as below A1: Hello World B1: orl How
Suppose i have to add numbers in the cell B1, F1, J1 giving a

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.