Double conversion to use prctile function

1 view (last 30 days)
Instead of evaluating column by column . I include a for loop so I can use the cell2mat function to convert from double to whatever data type the prctile function uses. However, after the for loop, I get an error message:
"Error using double
Conversion to double from cell is not possible.
Error in prctile (line 55)
x = double(x);
Error in E99 (line 45)
P = prctile(Model_mat(1,:),(99))"
But if I individually convert the column with the cell2mat function- the percentile function works fine.
% This is column by column
Model_mat1 = cell2mat(Model(1,:));
Model_mat2 = cell2mat(Model(2,:));
Model_mat3 = cell2mat(Model(3,:));
Model_mat4 = cell2mat(Model(4,:));
Model_mat5 = cell2mat(Model(5,:));
Model_mat6 = cell2mat(Model(6,:));
Model_mat7 = cell2mat(Model(7,:));
Model_mat8 = cell2mat(Model(8,:));
Model_mat9 = cell2mat(Model(9,:));
Model_mat10 = cell2mat(Model(10,:));
% This is the for loop
for i = 1:length(Model)
%Model_mat{i,:} = cell2mat(Model(i,:));
%Model_mat{i,:} = int64(Model(i,:));
Model_mat{i} = cell2mat(Model(i,:));
end
% This iis the syntax for the percentile
P = prctile(Model_mat(1,:),(99))
Any help would be appreciated.
  1 Comment
Rik
Rik on 15 Sep 2022
What exactly is your question? You seem to have a cell array of some sort, but you want the percentile function to do that conversion for you?
It would all be easier to understand if you attach your data to the question.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Sep 2022
I am not certain what ‘Model’ is, however if it resembles this array, indexing into it as a cell array is straightforward —
Model = mat2cell(randn(10), ones(1,10),10)
Model = 10×1 cell array
{[ 0.3559 0.8681 0.5171 -1.9330 1.6688 -1.1222 -0.5230 0.4412 -2.6904 -0.3102]} {[ -0.4756 -0.0911 -0.4101 -1.2918 -0.8179 -0.6191 -0.2680 1.0212 0.5873 -2.4094]} {[ 1.9852 0.2609 0.5998 -1.2972 -0.9622 1.9963 -0.4795 -0.4450 1.4728 -1.6797]} {[ 1.7691 -0.3796 0.8273 0.1806 -0.7725 0.8785 0.3614 0.4027 -1.0187 0.6261]} {[ 0.0359 1.2318 0.4570 1.4907 1.2762 0.3159 0.4088 -0.2682 0.9570 -0.1869]} {[-1.3947 -0.2354 2.6897 -0.0101 -1.2078 -0.1153 -1.3919 -1.0017 -1.7950 -2.0804]} {[ 1.1646 -0.1035 -0.6771 0.6717 -0.3764 -0.5092 -1.9939 -0.9529 -0.5511 0.5268]} {[ -0.1635 1.9250 0.8539 0.1680 0.1291 0.5319 0.2604 -1.3130 -0.1796 1.1728]} {[ 0.0794 0.6675 0.2235 1.7531 -0.9750 -0.3654 -0.1841 -0.5366 0.1033 0.6050]} {[ 0.4417 -0.3038 0.6669 2.1762 -0.8061 -0.5986 2.7666 0.6785 -0.6522 -1.3446]}
for k = 1:size(Model,1)
P(k) = prctile(Model{k,:},99); % Loop Approach
end
P
P = 1×10
1.6688 1.0212 1.9963 1.7691 1.4907 2.6897 1.1646 1.9250 1.7531 2.7666
P = prctile(cell2mat(Model),99,2) % Matrix Approach
P = 10×1
1.6688 1.0212 1.9963 1.7691 1.4907 2.6897 1.1646 1.9250 1.7531 2.7666
The loop approach would likely be appropriate if all the rows are not the same lengths. Otherwise, the matrix approach is likely more efficient.
.
  2 Comments
Star Strider
Star Strider on 16 Sep 2022
As always, my pleasure!
I very much appreciate your compliment!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!