how to delete value?

조회 수: 2 (최근 30일)
SM
SM 2020년 8월 19일
편집: Jiaming Xu 2022년 10월 26일
I have two arrays which are:
A=[1 1 4 2 1 3 1 3 2 2];
a=[1 3 1 2];
The outcome will be:
A=[4 1 1 3 2 2];
The very beginning elements in A turn empty based on the elements in a. It is possible by using loop and function find. Is there any other way? TIA
  댓글 수: 2
KSSV
KSSV 2020년 8월 19일
How you the answer using A and a?
Walter Roberson
Walter Roberson 2020년 8월 19일
Is the rule sort of like:
Look at the next element of a . Take the value there, and locate the first occurance of it in A, and change that to 0 in A. Then move on to the next element of a
Another way of saying that is that there are two 1's in a and the first two 1's in A are to be set to 0; there is one 2 in A and the first 2 in A is to be set to 0; there is one 3 in a and the first 3 in A is to be set to 0
??

댓글을 달려면 로그인하십시오.

답변 (1개)

Jiaming Xu
Jiaming Xu 2022년 10월 26일
편집: Jiaming Xu 2022년 10월 26일
I assume (1) your vector a indicates the numbers you want to delete in A and (2) you only delete the first number in A if any for each element in vector a.
You can use the following way to do so (delete the first element == i in A)
% A and a are given
for i = a
A(find(A==i,1))=[];
end
after run the code, you will have A as expected

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by