Removing rows except containing certain numbers of "33"

조회 수: 2 (최근 30일)
Smithy
Smithy 2023년 5월 26일
댓글: Stephen23 2023년 6월 1일
Hello everybody,
I hope to keep the row with the containing certain numbers of "33" of the last two digits in 1st column of result.
and I hope to remove other rows.
the expecting answer is [133 1133; 2, 4] in this case. ()
I tried with
result(~ismember(result(:,1),'33'), :) = []; % It does not work.
result(~contains(string(result(:,1)),"33"), :) = []; % 332 also is included in result variable.
but it doest not work. 332 also is included in result variable.
Is there a way to check wheter last two digits meet the condition?
x = [103 133 1133 1344 332 105 1105 15 53 511]';
y = [1 2 4 5 7 2 1 8 10 1]';
result = [x,y];
% How to remove the rows except containing certain numbers of 1st column of result
% if I would like to keep the row with the "33" of the last two digits in 1st column of result,
% 133 and 1133 accept the condition. and 332 does not meet the condition.
% so the expecting result will be [133 2; 1133, 4]
result(~ismember(result(:,1),'33'), :) = []; % It does not work.
result(~contains(string(result(:,1)),"33"), :) = []; % 332 also is included in result variable.
  댓글 수: 1
Stephen23
Stephen23 2023년 5월 26일
As Dyuman Joshi correctly wrote, mixing up text and numeric data will not help you.
Just stick to the numeric domain and use MOD or REM.

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

채택된 답변

Hiro Yoshino
Hiro Yoshino 2023년 5월 26일
You should use "pattern" to detect what you want in the strings as follows:
Your data:
x = [103 133 1133 1344 332 105 1105 15 53 511]';
y = [1 2 4 5 7 2 1 8 10 1]';
result = [x,y]
result = 10×2
103 1 133 2 1133 4 1344 5 332 7 105 2 1105 1 15 8 53 10 511 1
Define pattern
pat = "33";
TF = endsWith(string(result(:,1)),pat);
Extract what I want
result(TF,:)
ans = 2×2
133 2 1133 4
Does this fit what you want to achieve?
  댓글 수: 1
Smithy
Smithy 2023년 5월 26일
Thank you very much for your huge helps. I really really appreciate with it. It works really well for me.

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

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 5월 26일
You are trying to compare numeric data with character data. You will have to convert your initial data to do that comparison.
Instead, just use rem() or mod()
x = [103 133 1133 1344 332 105 1105 15 53 511]';
y = [1 2 4 5 7 2 1 8 10 1]';
%Checking condition - last two digits correspond to 33
idx = rem(x,100)==33;
%Values corresponding to the condition
result = [x(idx)';y(idx)']
result = 2×2
133 1133 2 4
  댓글 수: 1
Stephen23
Stephen23 2023년 6월 1일
+1 simpler and more efficient without the superfluous type conversions.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by