Deleting an entry from an array in MATLAB

In summary, to delete an entry from an array in MATLAB, you can use the delete command or remove function. This will remove the specified element and shift the remaining elements to fill the gap. Multiple entries can also be deleted by specifying their indices. Deleting an entry will decrease the size of the array by one, and attempting to delete from an empty array will result in an error. Unfortunately, there is no way to undo a deletion in MATLAB, so it must be manually reversed by re-entering the element or creating a new array.
  • #1
math8
160
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]?
 
Physics news on Phys.org
  • #2
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.
 
  • #3
Thanks!
 
  • #4
Or even just A( A==4) = [] will work.
 

FAQ: Deleting an entry from an array in MATLAB

How do I delete an entry from an array in MATLAB?

To delete an entry from an array in MATLAB, you can use the delete command or the remove function. Both of these options will remove the specified element from the array and shift the remaining elements to fill in the gap.

Can I delete multiple entries from an array in MATLAB?

Yes, you can delete multiple entries from an array in MATLAB by specifying the indices of the elements you want to delete within square brackets after the delete command or remove function.

Will deleting an entry from an array in MATLAB affect the size of the array?

Yes, deleting an entry from an array in MATLAB will decrease the size of the array by one. This means that the number of elements in the array will be reduced by one after the specified element is removed.

What happens if I try to delete an entry from an empty array in MATLAB?

If you try to delete an entry from an empty array in MATLAB, you will receive an error message. This is because there are no elements in the array to delete, so the delete command or remove function cannot be executed.

Can I undo a deletion of an entry from an array in MATLAB?

No, once an entry is deleted from an array in MATLAB, it cannot be undone. You will need to re-enter the deleted element or create a new array with the desired elements to reverse the deletion.

Back
Top