MATLAB returning the value of decimal numbers less than 1 as ZERO

10 vues (au cours des 30 derniers jours)
Hello all,
I am having significant problems with the floating point numbers while scripting in MATLAB.
I have a data sheet having more than 230000 rows and 33 columns. What I need to do is that if a particular condition satisfy in one column ( column 17) I have to find that particular row index where this condition satisfy and I have to extract the correspoding value in the same row of a diferent column ( column 19) and return it into a vector.
I am able to extract everything in to a new vector (it is a 139x1 matrix). However, all values which are below 1, naturally decimal numbers, return as 0 in my new matrix. Everything above one is returned properly. I've read many troubleshoot questions in this forum by using tolerance (I have successfully used tolerance method for comparing floating point decimals in other parts of the script) but nothing seem to work for this particular issue. Can anyone help me in this ? . I will grab a screenshot of a small part of the data as well as the code and put it here for your reference.
Any help is appreciated
As you can see in the below screenshot, I need to get the first instant of a negative number in column 17 (here -6.5021e+03) and the return the corresponding value from column 19 (here 0.0060). It repeats many times (139) over the 230000 rows. The below is the code I have used. I have extracted that particular column 19 and saved it as a 230000x1 variable vector called DisCap. s_dis is the new matrix where the data needs to be saved and indices_res is the row indices inside a for loop.
S_dis(j-1,:)=DisCap(indices_res(j-1));
This code should save all the values from the indices_res rows of DisCap.
Condition.png

Réponse acceptée

Guillaume
Guillaume le 9 Avr 2019
Two things:
1) Using a loop for what you want is inefficient and probably will lead to bugs if you're not careful. What you want can be achieved much more easily without a loop. I'm not exactly sure what you mean by the the first instant of a negative number. If you mean _get all values in column 19 for which column 17 are negative, it's simply:
s_dis = yourmatrix(yourmatrix(:, 17) < 0, 19); %get all values in column 19 for which column 17 is negative
If you mean the first negative value of a continuous run of negative values, it's a bit more complicated
isstartofarun = diff([false; yourmatrix(:, 17) < 0]) == 1; %find transitions from nonnegative to negative
s_dis = yourmatrix(isstartofarun, 19); %get matching values in column 19
If you mean something else, you'll have to be clearer.
2) The way matlab displays number has no bearing on what is truly stored in the matrix. Whatever is stored in the matrix will be unchanged after the above (or your for loop). However, depending on the magnitude of the numbers and the display format that you're using some numbers may display as 0. Their actual value will still be used for all calculation
You can change the display format of the command with the format command, personally I use format longg, and the display format of the variable browser in the view tab of matlab toolbar.
  4 commentaires
Guillaume
Guillaume le 9 Avr 2019
My point was that whatever method you're using, a loop is not the correct approach. As I've shown, it's trivially easy to get all elements of one column that match a condition in another column, whatver that condition is, using just one or two lines of code.
Sreekanth Nandakumar
Sreekanth Nandakumar le 10 Avr 2019
Thank you very much for the suggestion.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by