Error using floor() and ceil() for specific values between o and 1
Afficher commentaires plus anciens
Good afternoon,
I'm implementing a function that receive a value and look for this value inside of a matrix. The matrix have just integer values, but the value inside the function is not integer. To solve this problem i'm using the following idea:
%The function receive the value of beta;
%find the first index of the matrix that represents a bigger value
% (Ex: if beta is 8.5, try to find the index that gives the value 9)
index_beta_i_sup = find(Re_36e4 == ceil(beta)); %matrix
beta_i_sup = ceil(beta);
if isempty(index_beta_i_sup) %if the value of beta is not found, I increment +1 until find it
while isempty(index_beta_i_sup)
beta_i_sup = beta_i_sup+1;
index_beta_i_sup = find(Re_36e4 == beta_i_sup);
end
end
The same idea to try to find the lower index value inside the matrix:
index_beta_i_inf = find(Re_36e4 == floor(beta));
beta_i_inf = floor(beta);
if isempty(index_beta_i_inf)
while isempty(index_beta_i_inf)
beta_i_inf = beta_i_inf-1;
index_beta_i_inf = find(Re_36e4 == beta_i_inf);
end
end
When I run the problem using values: 1 < beta < 180 the function works perfectly.I put some prints to check if I run the code with beta <1, the following error appear.
beta= 8.700000e-01;
beta_sup= 1;
index_beta_i_sup = 53 %(inside the matrix - OK)
beta= 8.700000e-01;
beta_inf= 0;
index_beta_i_inf = 52 %(inside the matrix - OK)
| _*%this part is not suppose to be here in the code!*_|
*beta= 104;
beta_inf= 155;
index_beta_i_inf = 206*
Error: Index exceeds matrix dimensions.
My matrix has no 206 elements, that's why I have problem, but i didn't intend to make this last calculation, appear alone *(this beta=104). The problem is just for floor().
If I use the value of beta=-0.87, the problem will be in the ceil() function, that will return this beta=104.
Thanks for your support.
2 commentaires
dpb
le 12 Juin 2016
Are you simply looking for the nearest integer value in the array to a non-integer input? How about
y = interp1(Y,Y,beta,'nearest');
where Y is your array of integer values, beta is the looked for nearest value to be found.
Mateus Roberto Urio
le 12 Juin 2016
Réponse acceptée
Plus de réponses (1)
dpb
le 12 Juin 2016
" just trying to figure out the index of the nearest value inside the matrix (superior and inferior)."
Then look for that directly...
I assume Y is ordered --
iLess=find(Y<beta,1,'last'); % last Y < beta location
iPlus=find(Y>beta,1,'first'); % first Y > beta location
If there's no value satisfying the inequality, will return empty, []
3 commentaires
Guillaume
le 12 Juin 2016
I believe the OP wants the index of the nearest value greater/smaller than beta, not the first value that happens to be above/below beta.
dpb
le 12 Juin 2016
That's what he'll get with the single caveat of the array being ordered--it's the last under and the first after. Unless they're not ordered those will also be closest but in that case you've got to do a MIN() on the difference to find the nearest.
Mateus Roberto Urio
le 13 Juin 2016
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!