1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Deleting an entry from an array in MATLAB

  1. Jul 16, 2012 #1
    I want a code in MATLAB that produces A\{4}, where A={3,6,1,-3,4,8}.

    I know for instance:
    A=[ 3 6 1 -3 4 8];

    A(5)=[]; produces

    3 6 1 -3 8

    But what should I do if I don't know the index (in this case 5) for the entry '4'; and I want to get
    [ 3 6 1 -3 8]?
     
  2. jcsd
  3. Jul 17, 2012 #2

    jbunniii

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    You could try something like

    idx = find(A == 4);
    A(idx) = [];

    This will remove all instances of 4 from the array. If the array doesn't contain any 4's, then nothing will be removed.
     
  4. Jul 25, 2012 #3
    Thanks!!
     
  5. Jul 25, 2012 #4

    uart

    User Avatar
    Science Advisor

    Or even just A( A==4) = [] will work.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Deleting an entry from an array in MATLAB
Loading...