How do I search through a tall array element by element?

7 visualizaciones (últimos 30 días)
littlepho
littlepho el 10 de Abr. de 2017
Comentada: James Tursa el 12 de Abr. de 2017
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));

Respuesta aceptada

Edric Ellis
Edric Ellis el 11 de Abr. de 2017
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.
  3 comentarios
Edric Ellis
Edric Ellis el 12 de Abr. de 2017
The ability to use some forms of numeric subscripts in the first dimension (including this form) was added in R2017a - but you're right, this wouldn't work in R2016b.
James Tursa
James Tursa el 12 de Abr. de 2017
Yes, I was running R2016b ...

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 11 de Abr. de 2017
You can't test floating point numbers for equality. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
You can use ismembertol().

James Tursa
James Tursa el 11 de Abr. de 2017
Editada: Guillaume el 11 de Abr. de 2017
tall arrays are not "in memory". As such, they cannot be used for controlling "if" and "while" statements since their values (and results of operations on their values) isn't known to the code until they are "gathered" into memory. So you cannot do what you are trying to do the way you are trying to do it. If you really want to use tall array elements this way, you will need to gather subsets into memory so that the results can be used in "if" tests and "while" loops. E.g., see this deferred evaluation link:
Also, you can't use arbitrary indexes with tall arrays, so there will be restrictions on how you can pull the subsets out. See the doc.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by