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