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

6 次查看(过去 30 天)
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

回答(1 个)

Dyuman Joshi
Dyuman Joshi 2023-10-22
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-10-23
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".

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by