Sort Descend Scientific Notation Error

조회 수: 3 (최근 30일)
Kiran Eswaran
Kiran Eswaran 2019년 10월 14일
댓글: Stephen23 2019년 10월 15일
Hi,
I've got a vector of numbers. The vector has some numbers stored with and without scientific notation.
Eg 0.01 and 9.8e-04.
When i try sorting these numbers in descending order, the sort function puts 9.8e-05 above 0.01, even though 0.01 is larger.
Can anyone help me so it sorts from largest to smallest based on the expanded values? I've been trying to convert the numbers in the matrix from scientific notation to standard form, but changing format has not helped, and i haven't been able to get rid of the scientific notation properly.
I've looked through past questions / answers and have not found an answer that has worked for me.
Any advice is appreciated.
I believe the issue may be that the numbers are considered strings, but unsure how to convert string scientific notation to numbers.
Example data and code:
u =
[ APH 0.01
GPT 9.8e-04
CPH 6.4e-05]
[~,idx] = sort(u(:,2),'descend');
sortedu = u(idx,:);
sortedu looks like:
sortedu =
[GPT 9.8e-04
CPH 6.4e-05
APH 0.01]
Thanks,
Kiran
edit: updated code.
  댓글 수: 4
Kiran Eswaran
Kiran Eswaran 2019년 10월 14일
편집: Kiran Eswaran 2019년 10월 14일
Adding on to that point, the initial stock data were stored in a cell. I extracted from the cell as a string (string(cellname)), and then converted the string using str2double, as str2num didn't work for me.
  • str2double(string(cellname));
I guess a better way to frame my question:
How can i change the string scientific notation (eg 9.8e-04) to a number.
Cheers,
Kiran
Stephen23
Stephen23 2019년 10월 15일
@Kiran Eswaran : please upload your original data in a .mat file, by clicking the paperclip button. Note that I wrote "original data": this means your original table u (or whatever it is called), before you tried to do any conversions.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jos (10584)
Jos (10584) 2019년 10월 14일
편집: Jos (10584) 2019년 10월 14일
You misuse the sort command. Simply:
sortedu = sort(u ,'descend')
would do :-)
Moreover, if you remove the semicolons, you would see that you have some strange results in between...
Ask yourself what u(:,2) does, and u(idx,:).
  댓글 수: 1
Kiran Eswaran
Kiran Eswaran 2019년 10월 14일
Apologies, i framed the initial code incorrectly and have updated it.
I think the issue is that the numbers are stored as strings in scientific notation form (eg 9.8e-04) and so i'm unsure how to convert this string scientific notation to a number. I believe solving that conversion will solve the sort issue.
Cheers,
Kiran

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by