Effacer les filtres
Effacer les filtres

error with changing array size in loop

3 vues (au cours des 30 derniers jours)
Sonali
Sonali le 23 Oct 2023
Commenté : Sonali le 23 Oct 2023
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.

Réponses (1)

Walter Roberson
Walter Roberson le 23 Oct 2023
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 commentaire
Sonali
Sonali le 23 Oct 2023
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by