필터 지우기
필터 지우기

How to check if two elements are present in an array at once?

조회 수: 10 (최근 30일)
Jahnavi B R
Jahnavi B R 2021년 9월 2일
답변: Awais Saeed 2021년 9월 2일
I have two variables x and y they have numbers say,1 and 3. I want to check if these two numbers are not existing in an array named used1
If they (x and y) are not existing in the array, then my values x and y must be stored in used1. (because they weren't existing here before).
Currently using
If used1(:)~=x(:),y(:)
But it's only checking for value of x
If either of the values exist in this array, it should check in the next else if statement.

답변 (1개)

Awais Saeed
Awais Saeed 2021년 9월 2일
clc;clear all;close all;
x = [1 5];
y = 2;
used1 = [9 10];
A = [x y]; % concatenate x and y to check at once
used1 = [used1 A(find(~ismember(A,used1)))] % store un-used values in used1 arary
used1 = 1×5
9 10 1 5 2

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by