I want to extract rows from my matrix that have a specific value in the second column.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Philip Edwards
il 5 Lug 2023
Risposto: Kavita Guddad
il 5 Lug 2023
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 Commento
Kavita Guddad
il 5 Lug 2023
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)
Risposta accettata
Kavita Guddad
il 5 Lug 2023
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)
0 Commenti
Più risposte (1)
Abhas
il 5 Lug 2023
Modificato: Abhas
il 5 Lug 2023
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!
0 Commenti
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!