error in table2array - unable toconcatenate the specified table of arrays
8 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to develop a small MATLAB program that, in the end, would:
- Load a worksheet form a large workook
- select 2 columins
- compoute and plot a k-means cluster
the kernel of the code is:
vectorESIA = table2array(ESIAS25)
intensity = vectorESIA(:,6)
etnia = vectorESIA(:,7)
intensityCluster = kmeans(vectorESIA,4)
gscatter (intensity,etnia,intensityCluster)
xlabel('economic intensity')
ylabel ('ethnicity')
but it crashes on line 1 in the table2array
the table2array uses the table ESIAS25, which looks Ok, as per the screenshot below, the data is on column 6 & 7:
Can anyone help?
0 个评论
回答(2 个)
Abderrahim. B
2022-7-27
Hi!
table2array Converts table to homogeneous array. Your table has mix data (numric, text, ). table2cell is an option that you may consider for what you are trying to do !
HTH
0 个评论
Steven Lord
2022-7-27
You cannot turn this table into an array with table2array. Luckily, you don't need to turn it into an array.
The reason you can't turn this table into an array is because you have table variables of mixed types. The vilCode and vilName variables contains strings. The latGPS, longGPS, intensidadeE, and codetnia variables are likely double arrays (though I suppose codetnia could be a categorical array.) The etniaPredominante variable looks to be a categorical array. How would MATLAB store all those different types in a numeric array? The answer is it doesn't, it throws an error.
But let's look at the lines of code that actually try to use that array you want to create.
You can extract the intensity variable from the table directly using table indexing, specifically with curly braces or with the name of the variable.
% Replace this
intensity = vectorESIA(:,6)
% with this
intensity = ESIAS25{:, 6} % or
intensity = ESIAS25{:, 'intensidadeE'} % or
intensity = ESIAS25.intensidadeE
You can do the same with etnia:
% Replace this
etnia = vectorESIA(:,7)
% with this
etnia = ESIAS25{:,7} % or
etnia = ESIAS25{:, 'codetnia'} % or
etnia = ESIAS25.codetnia
If you use the variable names in your indexing, this will insulate you against the ordering of the variables in your data changing.
Now for your kmeans call, if you want to only use the numeric data in the clustering process use table indexing. As an easy way to extract just the numeric data from the table, I'm going to use a vartype object.
numericData = ESIAS25{:, vartype('numeric')}
Now you should be able to pass that numeric array into kmeans. I haven't tested this but I expect it to work.
intensityCluster = kmeans(numericData, 4)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!