Finding different values of a matrix in a single loop
Show older comments
I have this matrix
4 5
2 8
3 4
5 6
1 3
2 4
2 7
i need to find a number in the matrix wich has to be a variable. For example if i need the number 3 then matlab has to get the first row that contains 3 ( in this case 3 4) then it has to find the first row with the number of the other collumn and do this for the rest of the matrix and has to works for any matrix (n,2)
Starting with the number 3 i should get this :
3 4
2 4
2 7
and then put it on a vector : 3 4 2 7
Was only able to get this till now
cid= [4 5;
2 8;
3 4;
5 6;
1 3;
2 4;
2 7]
z=zeros(1,3);
v=zeros(1,3);
x=3
y=0;
for i=1:linhas
for j=1:colunas
if cid( i,j) == x
v(1,i) = cid(i,2)
z(1,i) = cid(i,1)
z(1:x) = 0;
x = v(i) ;
end
end
end
6 Comments
Timo Dietz
on 1 Dec 2020
You wrote: "number of the other collumn"
Which other coulmn do you mean in case you have a (n,3) - matrix?
KALYAN ACHARJYA
on 1 Dec 2020
"then it has to find the first row with the number of the other collumn"
What number, as the input is 3? Also how did you get it?
2 4
2 7
dpb
on 1 Dec 2020
OK, I see [3 4] and then the [2 4]. By what rule did you obtain the [2 7] ?
Bruno Baptista
on 1 Dec 2020
Bruno Baptista
on 1 Dec 2020
Bruno Baptista
on 1 Dec 2020
Accepted Answer
More Answers (1)
Read about ismember.
A = [ 4 5
2 8
3 4
5 6
1 3
2 4
2 7] ;
xi = 2 ;
[c,ia] = ismember(A(:,1),xi) ;
iwant = A(c,:)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!