Accumarray error: Second input val must be full numeric, logical, or char vector or scalar
Afficher commentaires plus anciens
I saw a code on one of the Q&A thread which addressed how to find duplicate values and calculate the average of their corresponding values.
TestCase2 is the table that contains 2 columns, One with values ranging from 0 to 8190 and column 2 consists of values ranging from -0.9831 to 19.3073. I have to find the duplicates in column 1 and average the corresponding values from column 2 and make a new table. The code below is what I wrote but it gives me the error.
[rpm, ia, idx]=unique(TestCase2(:,1),"stable");
torque=accumarray(idx,TestCase2(:,2),[],@mean);
graph=[rpm torque]
Error using accumarray
Second input val must be full numeric, logical, or char vector or scalar
I am new to MATLAB and will really appreciate the help.
Réponses (1)
Bruno Luong
le 31 Oct 2020
Modifié(e) : Bruno Luong
le 31 Oct 2020
Convert your table to regular array
A = table2array(TestCase2 )
Then do the rest on A.
2 commentaires
Abhyuday Rastogi
le 31 Oct 2020
The simpler and more efficient solution which does not duplicate all data in memory is to just use the correct curly braces to access the content of the table:
TestCase2{:,2}
% ^ ^ use curly braces to access table *content*
The parentheses you were using returns another table, which as the error message states is not a suitable input for accumarray. Which type of brackets to use is explained in the documentation:
Catégories
En savoir plus sur Axis Labels dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!