Deleting an entry from an array in MATLAB

  • Thread starter Thread starter math8
  • Start date Start date
  • Tags Tags
    Array Matlab
math8
Messages
143
Reaction score
0
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]?
 
on Phys.org
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.
 
Thanks!
 
Or even just A( A==4) = [] will work.
 

Similar threads

  • · Replies 31 ·
2
Replies
31
Views
4K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
14
Views
3K
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K