how can solve this problem : Conversion to cell from double is not possible.
Show older comments
what the error (Error using Untitled18 (line 12)
Conversion to cell from double is not possible.)
% Ensure all columns except the last one are numeric
for i = 1:(width(data) - 1)
if iscell(data{:, i})
data{:, i} = grp2idx(categorical(data{:, i}));
elseif ischar(data{:, i})
data{:, i} = grp2idx(categorical(data{:, i}));
end
end
9 Comments
Hi Ali,
The error you mentioned indicates that there is an attempt to assign a double value to a cell array, which is not allowed in Matlab. To resolve this issue, make sure ensure that the data types are compatible during the conversion process. To address this error and ensure that all columns except the last one are numeric, you can modify the code as follows:
% Ensure all columns except the last one are numeric
for i = 1:(width(data) - 1)
if iscell(data{:, i}) || ischar(data{:, i})
data{:, i} = num2cell(grp2idx(categorical(data{:, i})));
end
end
So, in the above updated code,it checks if the data in the column is either a cell array or a character array before conversion.So, if the data is non-numeric, we convert it to categorical values using grp2idx and then convert the result back to a cell array using num2cell and preventing the "Conversion to cell from double is not possible" error from occurring. Please let me know if you have any further questions.
ali hamzah
on 17 Jul 2024
Hi Ali,
If your intention is to calculate the fitness value of particles, then your code should be
fitnessVal = calculateFitness(particles(i, :));
because this function "calculateFitness" will determine the fitness value of a specific particle at index 'i'. So, by passing the particle's data stored in 'particles(i, :)' to the function, the fitness value can be computed and assigned to the variable 'fitnessVal'. Hope this should resolve your problem.
Please let me know if you have any further questions.
ali hamzah
on 17 Jul 2024
Umar
on 17 Jul 2024
Hi Ali,
If you don’t mind, I rather prefer answering questions on this platform. Sorry about that. Hope you understand.
@ali hamzah and @Umar -- it is far more legible and likely somebody will respond if you will format your code so it is able to be copied/executed and probably even more importantly, the formatting makes it far easier to follow/understand and thereby make corrections as/if needed.
@ali hamzah -- very few (if any) of the forum volunteers will go offsite; if you want help from these kind volunteers, do your utmost to provide all the necessary details here in as complete and easily used/read format.
@ali hamzah - what is the purpose/need in the larger picture for the above code snippet to begin with? By the use of width, it appears that the variable data is a table; if using a table, then referencing it as an array with the curlies "{}" is unnecessary coding effort and overhead--use the table addressing mechanisms built into the class directly.
If the intent is to convert selected table variables to categorical, rather than using a loop, use the features of the table such as
data=convertvars(data,@ischar,'categorical');
As always, it is extremely difficult to point in the right direction when all that is available is a (nonworking) code snippet in isolation without any explanation of the desired end result except for "make the error go away!".
Attaching a (smallish) example table and the desired end result will virtually always lead to a far better solution and you're liable to learn something useful about how to use MATLAB more effectively to boot...
ali hamzah
on 17 Jul 2024
Impossible to answer without the context of the problem trying to be solved...individual lines of code out of context make no sense--remember we can only see what you show us; we don't have the knowledge of what you're trying to do that you have...
Where are the data coming from and why are they NaN values in the first place? How are we to know what "properly" is for your specific missing data? It could be to ignore them, replace them with some constant (0 or otherwise), interpolate, ????
Again, provide a description of the overall problem being looked at and sufficient code and data that we can have at least a clue as to what it is that is being done/needs to be done.
Umar
on 17 Jul 2024
Edited: Walter Roberson
on 17 Jul 2024
Hi Ali,
Have you tried using the isnan function because it will help you to check for NaN values in the array X. If any NaN values are found, an error message will be displayed. For more information on how to use this function, please refer to
https://www.mathworks.com/help/matlab/ref/isnan.html#
Now, to handle missing data properly, you can either remove the rows or columns containing NaN values or replace them with a specific value using functions like fillmissing. Again, if you want to utilize this function in your code or need more information, please refer to
Hopefully, following these guidelines will help you resolve your problem. Good luck!
Answers (1)
Star Strider
on 17 Jul 2024
0 votes
Perhaps —
X = fillmissing(X(:), 'nearest');
.
Categories
Find more on Logical 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!