Deleting an entry from an array in MATLAB

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

The discussion focuses on deleting an entry from an array in MATLAB, specifically removing the value '4' from the array A = [3, 6, 1, -3, 4, 8]. The solution provided involves using the find function to locate the index of the value and then removing it with A(idx) = []. An alternative method is also suggested, which is A(A == 4) = [], effectively removing all instances of '4' from the array.

PREREQUISITES
  • Basic understanding of MATLAB syntax and array manipulation
  • Familiarity with MATLAB functions such as find
  • Knowledge of logical indexing in MATLAB
  • Experience with array operations in MATLAB
NEXT STEPS
  • Explore MATLAB array manipulation techniques
  • Learn about logical indexing in MATLAB
  • Investigate the use of the find function for locating elements
  • Study best practices for optimizing array operations in MATLAB
USEFUL FOR

MATLAB users, data analysts, and engineers looking to enhance their skills in array manipulation and data processing within 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]?
 
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
2K
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