working with numbers in the array names in for/ if loop

5 vues (au cours des 30 derniers jours)
Sonali
Sonali le 22 Oct 2023
Commenté : Dyuman Joshi le 26 Oct 2023
Hi
I am struggling with a program in matlab where I need to make arrays with numbers varying in their name in the loop itself:
For example: given dataset with columns: data_column_1, data_column_2, data_column_3 . . . upto 10
Lets say:
………………………….
For i=1:10
array_(i)= mean(data_column_(i) w.r.t data_column_1 ) %( for all 10 arrays )
end
………………………………..
Solution must be like
array_1 has mean of data in data_column_1 wrt data_column_1
array_2 has mean of data in data_column_2 wrt data_column_1
array_3 has mean of data in data_column_3 wrt data_column_1 and so on.
I will really appreciate some solution here. Thanks

Réponses (1)

Dyuman Joshi
Dyuman Joshi le 22 Oct 2023
Dynamically naming variables is not a good coding practice.
As you have numbers only, you should concatenate all the data in a numerical array and directly use mean with the specific dimension.
  5 commentaires
Dyuman Joshi
Dyuman Joshi le 23 Oct 2023
From what I understood by the description you gave, try this -
(I didn't understand the bit about the 10 bins.)
idx = findgroups(arr(:,1));
u = unique(idx);
c = zeros(numel(u),3);
for j=1:3
c(:,j)=splitapply(@mean,arr(:,j),idx);
end
figure
plot(c(:,1), c(:,2), 'b.')
hold on
plot(c(:,1), c(:,3), 'r.')
If this does not work, please attach the data for "arr".
Dyuman Joshi
Dyuman Joshi le 26 Oct 2023
Any updates, @Sonali?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays 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