How do I search through a tall array element by element?
7 次查看(过去 30 天)
显示 更早的评论
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));
0 个评论
采纳的回答
Edric Ellis
2017-4-11
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 个评论
Edric Ellis
2017-4-12
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.
更多回答(2 个)
Image Analyst
2017-4-11
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().
0 个评论
James Tursa
2017-4-11
编辑:Guillaume
2017-4-11
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!