필터 지우기
필터 지우기

Extract rows that their columns have specific values

조회 수: 2 (최근 30일)
LH
LH 2022년 10월 12일
답변: VBBV 2022년 11월 6일
Hi,
I have the following matrix:
A = [ 0.1 0 ;
0.5 0 ;
3 0 ;
0 0 ]
and I want to extract the rows that their columns elements are larger or equal to zero and less or equal to one:
for i = 1:size(A,1)
ind = (A(i,:) >=0) & (A(i,:) <=1);
A1 = A(ind,:)
end
The answer should be:
A1 = [0.1 0 ;
0.5 0 ;
0 0 ]
but my code does not give me the last row.
Any help would be appreicted.

답변 (2개)

KSSV
KSSV 2022년 10월 12일
A = [ 0.1 0 ;
0.5 0 ;
3 0 ;
0 0 ] ;
for i = 1:size(A,1)
ind = (A(i,:) >=0) & (A(i,:) <=1);
A1 = A(i,ind)
end
A1 = 1×2
0.1000 0
A1 = 1×2
0.5000 0
A1 = 0
A1 = 1×2
0 0

VBBV
VBBV 2022년 11월 6일
A = [ 0.1 0 ;
0.5 0 ;
3 0 ;
0 0 ];
for i = 1:size(A,1)
if (A(i,:) >= 0) & (A(i,:) <=1) %
A1(i,:) = A(i,:);
end
end
A1
A1 = 4×2
0.1000 0 0.5000 0 0 0 0 0

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by