Accumarray error: Second input val must be full numeric, logical, or char vector or scalar
이전 댓글 표시
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.
답변 (1개)
Bruno Luong
2020년 10월 31일
편집: Bruno Luong
2020년 10월 31일
Convert your table to regular array
A = table2array(TestCase2 )
Then do the rest on A.
댓글 수: 2
Abhyuday Rastogi
2020년 10월 31일
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:
카테고리
도움말 센터 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!