Hi guys, I hope someone can help me:
I have a vector. Suppose this:
A=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)'
Some values of this vector are related through a cell array, in such a way that a value of the vector is the index of a cell. I need to know if a value of the vector "a" is related to another value "b".
B={15, 12, 0, [11, 7], 9, 0, [1, 20, 21], 18, 7, 0, 16, [13, 14], 17, 4, 6};
a=2; b=7
In this case, 2 and 7 are related:
Index Value
2 12
12 13,14
13 17
17 Nan
14 4
4 7
So, a=5 and b=6; and a=4 and b=1 are also related.
Thanks to the whole community

5 commentaires

Matt J
Matt J le 12 Août 2023
The way you determine that a is related to b is not clear, at least not to me.
Sergio Rojas Blanco
Sergio Rojas Blanco le 12 Août 2023
Modifié(e) : Sergio Rojas Blanco le 12 Août 2023
Sorry, it's a little weird
Index 2->12. Now, I take this value (12) like the new index
Index 12->13 and 14. Now, I take one of theses (13, the first) and I follow the sequence
Index 13->17, I follow the sequence
Index 17->Nan. If there isn't any value in the cell or it's zero, the sequence (with 13) would end here. But now, I take the other value (14, the sencond) and I follow the sequence
Index 14->4. I follow the sequence
Index 4->7. I have arrived to "b" starting from "a"
I hope you understand a little better now.
Thanks
Dyuman Joshi
Dyuman Joshi le 12 Août 2023
By this logic, a=4 and b=1 are related.
B={15, 12, 0, [11, 7], 9, 0, [1, 20, 21], 18, 7, 0, 16, [13, 14], 17, 4, 6};
Idx 4 - 11
Idx 11 - 16
Idx 16 - NaN
Idx 4 - 7
Idx 7 - 1
Related.
Sergio Rojas Blanco
Sergio Rojas Blanco le 12 Août 2023
You're rigth. I have edited to remove the error
Dyuman Joshi
Dyuman Joshi le 12 Août 2023
Modifié(e) : Dyuman Joshi le 12 Août 2023
@Sergio Rojas Blanco, please check my answer.

Connectez-vous pour commenter.

 Réponse acceptée

A=(1:10)';
B={15, 12, 0, [11, 7], 9, 0, [1, 20, 21], 18, 7, 0, 16, [13, 14], 17, 4, 6};
m = max([B{:}]);
z1 = nested(A,B,2,7)
z1 = 1
%8th index is 18 which is greater than the number of elements
%so no value is related to it
z2 = nested(A,B,8,randi(m))
z2 = 0
%3rd index is 0, so no value related to it
z3 = nested(A,B,3,randi(m))
z3 = 0
%Similarly for 6th index
z4 = nested(A,B,6,randi(m))
z4 = 0
z5 = nested(A,B,5,6)
z5 = 1
%As the 1st index in B goes to the 6th index, no value will be related to it as well
z6 = nested(A,B,1,randi(m))
z6 = 0
function z = nested(A,B,a,b)
%Returns 0 if not related, 1 if related
%Initialize z as 0
z=0;
nB=numel(B);
for m=1:numel(a)
%Assign only if the value is within the range of number of elements of B
if a(m)<=nB && a(m)>0
c=B{a(m)};
else
%If the value is not in the range, skip to the next iteration
continue
end
%Check if b is present in c or not
if ismember(b,c)
z=1;
return
else
%If not present, continue the recursion and keep checking
z=nested(A,B,c,b);
end
end
end

2 commentaires

Sergio Rojas Blanco
Sergio Rojas Blanco le 12 Août 2023
Thanks a lot Dyuman. I have gained a lot of time with your code.
Dyuman Joshi
Dyuman Joshi le 12 Août 2023
Glad to have helped!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by