Filter löschen
Filter löschen

selecting particular rows in a matrix

1 Ansicht (letzte 30 Tage)
Naga A
Naga A am 13 Okt. 2015
Kommentiert: Star Strider am 13 Okt. 2015
Hi , I know this is very simple, I have a matrix looks like
A =
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
I need to select the rows which contains only one 1's like the rows 1,2,4. next i need to select the rows which contains two ones like the rows 3,5,6.I'm using the function find to check the indexes containing one as follows:
for i=1:length(A)
b=find(A(i,:)==1);
c(i)=length(b);
end
Is there any possible solution instead of using the for loop, because I need to use this code for the similar matrix contains 120 columns? Thanks for the help.

Akzeptierte Antwort

Star Strider
Star Strider am 13 Okt. 2015
This works:
A = [0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1];
Rows_1 = find(sum(A,2)==1) % Rows with 1 ‘1’
Rows_2 = find(sum(A,2)==2) % Rows with 2 ‘1’s
Rows_3 = find(sum(A,2)==3) % Rows with 3 ‘1’s
  2 Kommentare
Naga A
Naga A am 13 Okt. 2015
Thank you.
Star Strider
Star Strider am 13 Okt. 2015
My pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by