Deleting an entry from an array in MATLAB

  • Thread starter Thread starter math8
  • Start date Start date
  • Tags Tags
    Array Matlab
Click For Summary

Homework Help Overview

The discussion revolves around manipulating arrays in MATLAB, specifically focusing on how to delete an entry from an array when the index of that entry is not known. The original poster presents an example array and seeks guidance on achieving the desired outcome without directly referencing the index of the element to be removed.

Discussion Character

  • Exploratory, Problem interpretation

Approaches and Questions Raised

  • Participants explore different methods to identify and remove elements from an array based on their value rather than their index. Suggestions include using the 'find' function and logical indexing.

Discussion Status

Several approaches have been proposed, including using the 'find' function and logical conditions to remove elements. Participants have acknowledged these methods, but there is no explicit consensus on a single preferred solution.

Contextual Notes

The original poster's inquiry is framed within the context of MATLAB programming, and there is an implicit understanding that the removal of elements should be done without prior knowledge of their indices.

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]?
 
Physics news 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
3K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
14
Views
2K
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K