Check if a value exists in a cell array starting from a value whose index is the desired value
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
Let's suppose:
A={5, 15, 0, [11, 7], 9, 0, [1, 20, 21], 18, [22,15], 0, 16, [13, 14], 17, 4, 6 };
a=6; b=1;
Now, I would like to know if I can get to b from a using the values as an index. In this case:
Value Index
6 -> 15
15 -> [2, 9]
for
2 -> Nan
9 -> 5
and
5 -> 1
Thanks
Accepted Answer
Dyuman Joshi
on 14 Aug 2023
I have unsuppressed the output of variable temp to show the path that is followed (as you have demonstrated above) -
A={5, 15, 0, [11, 7], 9, 0, [1, 20, 21], 18, [22,15], 0, 16, [13, 14], 17, 4, 6 };
a=6; b=1;
B = [A{:}];
%maximum value present in A
m = max(B);
z1 = nested(A,6,1)
%Any value greater than the maximum value in the cell array will not be present
%So for any value of b, the output will be zero
z2 = nested(A,m+randi(100),randi(numel(A)))
z2 = 0
z3 = nested(A,9,4)
temp = 5
temp = 1
temp = 7
temp = 4
z3 = 1
%3rd value corresponds to zero, so the search is terminated for any value in A
z4 = nested(A,3,B(randi(numel(B))))
temp =
1×0 empty double row vector
z4 = 0
function z = nested(A,a,b);
%Returns 0 if not related, 1 if related
%Initialize z as 0
z=0;
nA=numel(A);
%Values corresponding to indices of elements in A
idx=1:numel(A);
%Check if b is present in a or not
if ismember(b,a)
z=1;
return;
%Return 1 if present, else continue checking
else
for k=1:numel(a)
%Check only if the values is within the range of number of elements of B
if a(k)>0 && a(k)<=nA
%Finding the index of the value
y=cellfun(@(x) ismember(a(k),x), A);
%Corresponding values
temp=idx(y)
z = nested(A,temp,b);
else
%If the value is not in the range, skip to the next iteration
continue;
end
end
end
end
6 Comments
You are a genius! You have a new friend for ever :D
Thanks a lot.
Hahaha, I'm not a genius, but yeah happy to have a new friend :)
Glad to have helped!
Note that this function will result in an infinite recursion for some inputs, e.g.:
A = {1,2};
a = 2;
b = 1;
nested(A,a,b)
Sergio Rojas Blanco
on 14 Aug 2023
Edited: Sergio Rojas Blanco
on 14 Aug 2023
You are right. I have found this situation.
Dyuman Joshi
on 14 Aug 2023
Edited: Dyuman Joshi
on 14 Aug 2023
I have edited it, this should work -
A = {1, 2};
a = 2;
b = 1;
z = nested(A,a,b)
z = 0
function z = nested(A,a,b);
%Returns 0 if not related, 1 if related
%Initialize z as 0
z=0;
nA=numel(A);
%Values corresponding to indices of elements in A
idx=1:numel(A);
%Check if b is present in a or not
if ismember(b,a)
z=1;
return;
%Return 1 if present, else continue checking
else
for k=1:numel(a)
%Check only if the values is within the range of number of elements of B
if a(k)>0 && a(k)<=nA
%Finding the index of the value
y=cellfun(@(x) ismember(a(k),x), A);
%Corresponding values
temp=idx(y);
%If temp is equal to a, return 0, avoiding infinite recurison
if isequal(temp,a)
return;
end
z = nested(A,temp,b);
else
%If the value is not in the range, skip to the next iteration
continue;
end
end
end
end
More Answers (0)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Tags
See Also
on 14 Aug 2023
on 14 Aug 2023
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)