MATLAB: How to search through a tall array element by element

faqtall array

I would like to be able to search through a tall array for a specific item. However, it seems any logical operator cannot be used with tall arrays. For example:
A = rand(1000,1);
tA = tall(A);
for i=1:1000
if tA(i) == 0.5
disp('i is equal to 0.5')
end
end
This code will result in a 'conversion to logical from tall is not possible' error. So is there a way to search through a tall array without using subsets of the array, such as:
tAsubset = gather(tA(1:100));

Best Answer

You can make this code work by writing
if gather(tA(i) == 0.5)
disp('i is equal to 0.5')
end
however this will be incredibly inefficient - each call to gather needs to pass over the data. You can use the == operator on the whole array to get a tall logical result.
isHalf = (tA == 0.5);
Or, bearing in mind the sound advice from @Image Analyst, you could use a tolerance (unfortunately tall arrays don't support ismembertol)
isRoughlyHalf = (tA >= 0.49 & tA <= 0.51);
The real question is what do you want to do next with all the entries that satisfy the criterion? You could gather just those using logical indexing
dataSubset = tA(isRoughlyHalf);
gather(dataSubset);
or perform some other operation on the subset of data.
Related Question