Reputation: 3
In my array of strings, I have some values like "8.1076e-05". The problem I encountered while shorting. I used the code FinalHierarchy = sortrows(B, 3, 'descend');
to short, the result is below.
"FC624 " "0.010686" "9.5329e-05"
"FC642 " "0.0044959" "8.2636e-05"
"FC621 " "0.0090886" "8.1076e-05"
"FC622 " "0.0086253" "7.6943e-05"
"FC623 " "0.0080882" "7.2152e-05"
"FC643 " "0.0028437" "5.2267e-05"
"FC646 " "0.0025469" "4.6813e-05"
"FC644 " "0.0012072" "2.2189e-05"
"FC122 " "0.0073847" "0.00077775"
"FC142 " "0.010012" "0.00072479"
"FC131 " "0.0094101" "0.00071951"
"FC121 " "0.0068294" "0.00071926"
"FC521 " "0.0095355" "0.00069401"
"FC112 " "0.0087711" "0.00063404"
"FC111 " "0.008607" "0.00062218"
"FC531 " "0.0058125" "0.00055105"
"FC141 " "0.0074634" "0.0005403"
"FC228 " "0.0098074" "0.00049734"
"FC224" "0.009434" "0.0004784"
"FC221 " "0.0092251" "0.00046782"
"FC225" "0.0088874" "0.00045069"
It can be seen above that MATLAB has considered the value 2.2189e-05
bigger than 0.00077775
. I guess the reason behind it is, MATLAB compared 2.2189
with 0.00077775
. It is because the column is a string. In my calculation, column 2
and column 3
were double but when I concatenated them with a string column 1
as 'B'
, it became string.
I guess it might have a very easy solution if 2.2189e-05
stays as 0.00002219
. I try to change the format
to longG
but the problem still persists.
Can someone suggest any idea to deal with it?
Upvotes: 0
Views: 68
Reputation: 1163
As you said, it is because your elements are strings. So what you could do is:
% Sort the column after having casted it to double
[~, idx] = sort(str2double(B(:, 3)), 'descend');
% Apply the sorting to the initial matrix
FinalHierarchy = B(idx, :);
Upvotes: 1