error with changing array size in loop

2 次查看(过去 30 天)
Sonali
Sonali 2023-10-23
评论: Sonali 2023-10-23
I have a loop running as follows:
..............................................................................................................................................
for i= 1:10
if isempty( arr) )==0
for j=1:3
c(:,j)=splitapply(@mean,arr(:,j),findgroups(arr(:,1))); %<----------- error line.
end
c
plot(log(c(:,2)),c(:,1),'b.')
hold on
end
end
.................................................................................................
During each iteration(i), 'c' variable has a different size(i.e. number of elements are changing). Because of which I get following error:
[ Unable to perform assignment because the size of the left side is 3-by-1 and the size of the right side is 4-by-1. ]
Could someone help me in solving this issue? How may I make this array/variable dynamic so that it can store and plot any values in nx3 dimensions. I would really appreciate some help.

回答(1 个)

Walter Roberson
Walter Roberson 2023-10-23
What reason should we have to expect that there will always be the same number of different groups? splitapply() called that way is going to return one value per group that is found.
Also, findgroups() for numeric data is, in practice, going to create group numbers sorted by the numeric value associated with the group. If in one call the assorted values include (say) 0.1 0.11 0.2 and the next iteration the assorted values include (say) 0.12 0.11 0.2 then in the first iteration group 1 would be for 0.1 and group 2 would be for 0.11 -- but in the second iteration 0.11 would be group 1 instead of group 2. Therefore even if the values are related between iterations, the order might well change, which is likely to cause confusion.
  1 个评论
Sonali
Sonali 2023-10-23
Got it. I have data that is segregated into several bins. For each bin, I am just trying to calculate the average of all columns wrt the unique values of first column. Is there a better way of doing this? Kindly suggest. Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by