Delete string element in array (Matlab)

I have a string: pairs = ['AA';'AB';'BB';'AC'; 'CC'; 'CB';'DE'; 'BC']

How can I delete the element which has the same characters in its string like 'AA','BB', 'CC' ?

The expected output should be: out = ['AB';'AC';'CB';'DE';'BC']

-------------Problems Reply------------

Use logical indexing and compare first and second column:

out = pairs(pairs(:,1)~=pairs(:,2),:)

For a more general way (to cover rows with more than two characters) you can create the index of rows that have all elements equal to each other using bsxfun:

allsame = any(~bsxfun(@eq, pairs, pairs(:,1)), 2);
out = pairs(allsame,:);

If all the entries are only 2 elements, you could subtract each elements and if the result is 0 then both elements are the same.


pairs = {'AA';'AB';'BB';'AC'; 'CC'; 'CB';'DE'; 'BC'}

Diffs = cellfun(@(x) diff(x),pairs)

Diffs looks like this:

Diffs =


Now delete those entries:

pairs(~Diffs) = []

pairs =


Category:string Views:0 Time:2019-03-14

Related post

  • C# How to add string elements in array to string 2009-12-27

    Need assistance to do this. Or advice about how to add string to string elements in array? We have 2 txt files with 100 or more strings in each: domain.txt domain1 domain2 .. title.txt title1 tilte2 .. and we have: string link = < a h ref="http://

  • Delete dictionary element from array (plist) Cocoa Touch 2011-03-03

    Hey guys, I have this plist: <plist version="1.0"> <dict> <key>Rows</key> <array> <dict> <key>FavTitle</key> <string>Book1</string> <key>Favourited</key> <string>duh</st

  • remove element from array matlab 2012-03-15

    i have a array that contains all the files in a particular directory. I want to remove all the file entries that end with the .txt extension. This is what i have written function fileList = removeElements(fileArray) for idx = 1:numel(fileArray) if (s

  • Java: Non-destructively deleting first element of array? 2012-01-29

    What I am trying to do is convert this C++ function to Java: void print(int x[], int l) { if (l != 0) { cout << x[0] << ","; print(x+1,l-1); } } This is what I am trying to do, but it won't work: void print(int x[]) { if (x.length != 0) {

  • Adding string Elements in NSArray 2012-04-07

    I have mutable string array named arrayout. It is having 3 element .Now I want to add 1 String element that array.But when I try to add,it is taking null value....Cant get what to do...Please help... My code is : NSString *ds1 = @"--"; [arrayout arra

  • Delete an element from an array 2008-12-15

    Is there an easy way to delete an element from a PHP array, such that foreach ($array) no longer includes that element? I thought that setting it to null would do it, but apparently not. --------------Solutions------------- You use unset: <?php $x

  • How to delete an element from an array in C# 2009-01-30

    Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2}; How can I delete an element by "name"? , lets say number 4? Even ArrayList didn't help to delete? string strNumbers = " 1, 3, 4, 9, 2"; ArrayList numbers = new ArrayList(strNumbers.Split(ne

  • How can I delete an element in my array? 2011-06-26

    I am having a hard time deleting an element in an Array. I have tried for a while and I am beating my head against the desk trying to figure this out. I would greatly appreciate any help. Thanks. package javaapplication9; import java.util.Scanner; im

  • Delete element from array if not found 2011-12-21

    I have some code I created which is supposed to see if something exists in an array of strings. If it does not exist, I want to delete that element of the array. I thought we do this with unset, but it doesnt seem to be working. Mind helping? echo '

  • c++ deleting 1 element of dynamic array? 2012-03-01

    Im working on a program that inputs book data in a struct, and deletes an element upon the user's request. However, Im having difficulty figuring out the best way to do delete the requested book. Do you think Im headed in the right direction? struct

  • How to remove strings from all elements in array? 2009-11-17

    How can i remove a repeating string keyword from all elements in an array ? --------------Solutions------------- I think you mean you have an array of strings and they all contain some substring that you want to remove. Non-destructively: {

  • C# adding string elements of 4 different string arrays with each other 2010-03-13

    I need an advice about how to create new string array from 4 different string arrays: We have 4 string arrays: string[] arr1 = new string [] {"a1","a2","a3"..., "a30"}; string[] arr2 = new string [] {"d10","d11","d12","d13","d14","d15"}; string[] arr

  • Returning the element number of the longest string in an array 2010-06-14

    I'm trying to get the longest method to take the user-inputted array of strings, then return the element number of the longest string in that array. I got it to the point where I was able to return the number of chars in the longest string, but I don

  • code does not insert all elements of array to string 2010-12-13

    i have this code which should insert all elements of array which is some html on random places in a string. but it only inserts last element to that string. please help Random insertPos = new Random(); int pos = insertPos.Next(txtInput.Text.Length);

  • How to delete specific element from an array? 2011-01-24

    How can i delete a specified element from an array? for example i added elements from an array like this: int[] array = new int[5]; for (int i = 0; i < array.Length; i++) { array[i] = i; } How to delete the element from index 2? --------------Solu

  • Insert/delete/retrieve elements of an array across threads without using globals 2011-02-05

    This might be a silly question, but what's the best way to keep insert/delete/retrieve elements of an array in a multi-threaded Python application, but not keep the array global or assign an entire thread to keep track of the changes to that single a

  • Add prefix to each element of array of strings 2011-05-05

    Is there a quick way to add a string prefix to each element in array of strings? --------------Solutions------------- Define a category of NSMutableString @implementation NSMutableString (AppendPrefix) - (void)appendPrefix:(NSString *)prefix { [self

  • How to delete an element from an XML tree where attribute is specific string in Simple XML PHP 2011-09-06

    So I want to delete a child from an XML string where an attribute is a specific value. For Example: <xml> <note url=""> Values </note> <note url=""> Yahoo Values </note> </xml> So h

  • tr1::array in C++ deleting all elements 2011-10-14

    how to delete all elements of a std::tr1::array? For example I have defined the following array. std::tr1::array <int, 5> a = { 1, 2, 3, 4, 5} Just like in vectors I am looking for a method such as a.clear() or a.erase() but couldn't find one f

  • How to split a string into an array in jQuery when there is only one element in the string? 2012-01-02

    I have this code, that takes a string and splits it into an array: nodes = $("#" + model_id + "-" + node_class + "--" + "title").data("nodes").split(",") When there is only one element in the string (no commas), the variable "nodes" does not become a

  • array_map and append string to elements of array 2012-01-14

    I have an array like this: $a = array('aa','bb','cc','dd'); I want to add 'rq' string at the begging of all the elements of array. Is it possible to do it with calling array_map() on this array? --------------Solutions------------- $a = array_map(fun

  • In c# array of strings, the individual elements of array are reference type or value type? 2012-03-16

    Since in any array , array itself is a reference type and individual elements of array are value type ... is this the case with array of strings too? --------------Solutions------------- System.String is a reference type. The items in an array are va

  • Elisp: How to delete an element from an association list with string key 2012-03-21

    Now this works just fine: (setq al '((a . "1") (b . "2"))) (assq-delete-all 'a al) But I'm using strings as keys in my app: (setq al '(("a" . "foo") ("b" . "bar"))) And this fails to do anything: (assq-delete-all "a" al) I think that's because the st

  • php delete element from array 2012-03-30

    Im using cakephp 2.0 and i have data submitted that i want to cleanup, the array structure is below how do i delete the element (quoteitem) where the quantity=null? i have this but it doesnt work; foreach($this->request->data['Quoteitem'] as $q

  • Find maximum occurance of string element in an array in javascript 2014-05-15

    I have an Array of string elements and I need to find how many times a elements occured in a Array. my Array is following: var x=["water","water","water","land", "land","land","land","forest"]; I need to know which element is prominent in Array. I ha

Copyright (C), All Rights Reserved.

processed in 0.063 (s). 11 q(s)