필터 지우기
필터 지우기

I want to extract rows from my matrix that have a specific value in the second column.

조회 수: 4 (최근 30일)
I want to take all of the rows from a matrix that have a 1 in the second column and create a new matrix with these rows.
  댓글 수: 1
Kavita Guddad
Kavita Guddad 2023년 7월 5일
x=[1 2 3 5;2 3 4 5;2 1 3 4;2 1 3 2 ];
y=[];
for i=1:size(x,1)
if (x(i,2)==1)
y=[y;x(i,:)];
end
end
disp(x)
1 2 3 5 2 3 4 5 2 1 3 4 2 1 3 2
disp(y)
2 1 3 4 2 1 3 2

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

채택된 답변

Kavita Guddad
Kavita Guddad 2023년 7월 5일
Hope this code does your job.
x=[1 2 3 5;2 3 4 5;2 1 3 4;2 1 3 2 ];
y=[];
for i=1:size(x,1)
if (x(i,2)==1)
y=[y;x(i,:)];
end
end
disp(x)
disp(y)

추가 답변 (1개)

Abhas
Abhas 2023년 7월 5일
편집: Abhas 2023년 7월 5일
Hi Philip,
We can use logical indexing to select only the rows where the second column has a value of 1. The below code can solve your query:
% Sample input matrix
inputMatrix = [2, 1, 3;
4, 1, 6;
7, 0, 9;
1, 1, 2;
8, 1, 5];
% Find rows with a 1 in the second column
rowsWithOne = inputMatrix(inputMatrix(:, 2) == 1, :);
Hope this helps!

카테고리

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

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by