Finding different values of a matrix in a single loop

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 commentaires

You wrote: "number of the other collumn"
Which other coulmn do you mean in case you have a (n,3) - matrix?
"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
dpb le 1 Déc 2020
OK, I see [3 4] and then the [2 4]. By what rule did you obtain the [2 7] ?
sorry, its not (n,3), its (n,2).
if i start with 3 then it has to find the first row with the number 3 wich is [3 4]. So now that it found the row with 3 i want it to search the first row with the number 4( its number 4 cause it was on the other collumn of the row with 3).
it has to work for every matrix (n,2); i written 3 for mistake.
last result isnt [2 8] because i want it to start over from the previous row.
i obtain obtain the [2 7] because after it gets [2 4] then it has to search the number in the other collumn wich is 2

Connectez-vous pour commenter.

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 1 Déc 2020
Modifié(e) : Ameer Hamza le 1 Déc 2020
Edit: This is modified to follows the rule as you described in your comments.
A = [
4 5
2 8
3 4
5 6
1 3
2 4
2 7];
a = 3;
M = [];
m = a;
count = 1;
col = 1;
while true
[idx, ~] = find(A==a, 1);
if isempty(idx)
break;
end
M(count, :) = A(idx, :);
count = count + 1;
a = setdiff(M(end, :), a);
m(count) = a;
A(1:idx, :) = [];
end
Result
>> M
M =
3 4
2 4
2 7
>> m
m =
3 4 2 7

9 commentaires

Awesome!
for it to be correct m should be giving [ 3 4 2 7 ]
Try the updated code.
Bruno Baptista
Bruno Baptista le 1 Déc 2020
Modifié(e) : Bruno Baptista le 1 Déc 2020
seems to work only when 3 is the origin number, when its 4 for example its shows an incomplete vector
It gives output m = [4 5] if you set a = 4. What is the expected output? There is no other 5 in second column.
i expected m = [4 5 6] , theres still one 5 in the first column
The rule is still a bit confusing. See the updated code in my answer. Is that what you want?
i think it works now, thks for the effort
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (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,:)
iwant = 3×2
2 8 2 4 2 7

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by