开发者

Slicing an Array

开发者 https://www.devze.com 2023-04-07 17:25 出处:网络
I am having trouble finding a matlab function to slice an element out of an array. For example: A = [1, 2, 3, 4]

I am having trouble finding a matlab function to slice an element out of an array.

For example:

A = [1, 2, 3, 4]

I want to take开发者_Python百科 out on element of this array, say the element 3:

B = [1, 2, 4]

Is there a matlab function for this or would I have to code the algorithm to construct a new array with all the elements of A except 3?


Do this:

index_of_element_to_remove = 3;
A(index_of_element_to_remove) = [];

now A will be [1 2 4]

If you want to remove more elements at the same time you can do:

index_of_element_to_remove = [1 3];
A(index_of_element_to_remove) = [];

now A will be [2 4]


By value, which will remove all elements equal to 3

A(find(A==3)) = []

Or by index

A(3) = []
0

精彩评论

暂无评论...
验证码 换一张
取 消