# In an assignment A(:) = B, the number of elements in A and B must be the same.

1 view (last 30 days)
Lopez Cristina on 15 Jul 2021
Answered: Steven Lord on 15 Jul 2021
Does anybody help me with this error?
I have this error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in bor_getIndexesRange (line 13)
indexes(2) = find(abs(data.(field) - rangeLimits(2)) < tolerance);
Here is the code:
function [ indexes ] = bor_getIndexesRange(data, rangeLimits, field)
% field = {'time'} or field = {'freq'}
indexes = [-1 -1];
% Tolerance in order to fix float points numbers comparision issues
if strcmp(field, 'time')
tolerance = 0.001;
elseif strcmp(field, 'freq')
tolerance = 0.2;
end
indexes(1) = find(abs(data.(field) - rangeLimits(1)) < tolerance);
indexes(2) = find(abs(data.(field) - rangeLimits(2)) < tolerance);
assert(~isempty(indexes(1)));
assert(indexes(1) > 0);
assert(~isempty(indexes(2)));
assert(indexes(2) > 0);
end

Steven Lord on 15 Jul 2021
indexes(1) = find(abs(data.(field) - rangeLimits(1)) < tolerance);
indexes(2) = find(abs(data.(field) - rangeLimits(2)) < tolerance);
These lines assume that there is exactly one, no more and no fewer, elements of the array that are less than the tolerance. If that assumption is violated (if multiple elements are within tolerance of the limits or if no elements are), those lines will error.
It's as though I asked you for one of the numbers in the vector [1 2 3 4 5] that is greater than 42. As Cleve Moler said, "The hardest things to compute are things that do not exist."
assert(~isempty(indexes(1)));
indexes(1) is a 1-by-1 array. By the definition of isempty it cannot be empty and so there is no way for this assert call to fail (unless you change the definition of the ~ operator, the isempty function, or what indexing means for the indices array.