필터 지우기
필터 지우기

Checking if all strings exist in row of array

조회 수: 14 (최근 30일)
Austin Welch
Austin Welch 2018년 7월 1일
댓글: Jan 2018년 7월 9일
So I have an array that has rows of strings(A) and I want to check if another user inputted array of strings(B) is contained in each row of (A). I believe I have it so that it will be true if at least one of the strings in array B is present, but I want them all to have to be present. I put an example below of what I am doing now in a shorter version and it is using numbers instead of strings so I know contains is not for numbers, but I just was trying to be quick. Thanks for your help!
v = [5 4 3 2 1; 4 3 2 6 6; 8 5 0 9 6];
f = input('please input nums to test'); % say it repeats
%and you input an array like [2 6 4]
for i = 1:3
% I want 2, 6, and 4 for to all have to be be in the row for it
% to be true not just one.
if contains(v(i), f) == 1
%Do something
end
end

답변 (1개)

Stephen23
Stephen23 2018년 7월 1일
편집: Stephen23 2018년 7월 1일
You could use ismember and all:
mat = [5 4 3 2 1; 4 3 2 6 6; 8 5 0 9 6];
str = input('Please enter numbers to test: ','s');
vec = sscanf(str,'%f',[1,Inf]);
for k = 1:size(mat,1)
if all(ismember(vec,mat(k,:)))
...
end
end
  댓글 수: 3
Stephen23
Stephen23 2018년 7월 1일
ismember also works with string arrays. Please show the code that you tried.
Jan
Jan 2018년 7월 9일
@Austin Welch: Posting inputs with a different data type does not simplify the problem, but leads to wrong solutions.

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

카테고리

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

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by