Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
Sonali
Sonali am 22 Okt. 2023
Kommentiert: Dyuman Joshi am 26 Okt. 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

Antworten (1)

Dyuman Joshi
Dyuman Joshi am 22 Okt. 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 Kommentare
Dyuman Joshi
Dyuman Joshi am 23 Okt. 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".

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by