Clear Filters
Clear Filters

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

6 views (last 30 days)
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

Answers (1)

Dyuman Joshi
Dyuman Joshi on 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 Comments
Dyuman Joshi
Dyuman Joshi on 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".

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!