如何在 MATLAB 循环中创建/处理变量?

发布于 2024-09-07 17:43:25 字数 453 浏览 7 评论 0原文

我需要计算多个变量的平均值、标准差和其他值,我想知道如何使用循环来发挥我的优势。我有 5 个电极的数据。因此,为了计算每个的平均值,我这样做:

mean_ch1 = mean(ch1);  
mean_ch2 = mean(ch2);  
mean_ch3 = mean(ch3);  
mean_ch4 = mean(ch4);  
mean_ch5 = mean(ch5);  

我想要的是将代码压缩成一行左右。我尝试的代码不起作用:

for i = 1:5  
  mean_ch(i) = mean(ch(i));  
end

我知道这段代码是错误的,但它传达了我想要完成的任务的想法。我希望最终得到 5 个单独的变量,这些变量由循环或元胞数组命名,其中包含所有 5 个变量,以便于调用。我知道必须有一种方法来编写这段代码,我只是不确定如何完成它。

I need to calculate the mean, standard deviation, and other values for a number of variables and I was wondering how to use a loop to my advantage. I have 5 electrodes of data. So to calculate the mean of each I do this:

mean_ch1 = mean(ch1);  
mean_ch2 = mean(ch2);  
mean_ch3 = mean(ch3);  
mean_ch4 = mean(ch4);  
mean_ch5 = mean(ch5);  

What I want is to be able to condense that code into a line or so. The code I tried does not work:

for i = 1:5  
  mean_ch(i) = mean(ch(i));  
end

I know this code is wrong but it conveys the idea of what I'm trying to accomplish. I want to end up with 5 separate variables that are named by the loop or a cell array with all 5 variables within it allowing for easy recall. I know there must be a way to write this code I'm just not sure how to accomplish it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗失的美好 2024-09-14 17:43:25

您有几种选择来执行此操作:

  • 您可以先将所有通道数据放入一个大矩阵中,然后使用函数 平均值。例如,如果每个 chX 变量都是 N×1 数组,您可以执行以下操作:

    chArray = [ch1 ch2 ch3 ch4 ch5]; %# 创建一个 N×5 矩阵
    均值数组 = 均值(chArray); %# 取每列的平均值
    
  • 您可以先将所有通道数据放入元胞数组中,然后计算使用函数 CELLFUN:

    的每个单元格

    meanArray = cellfun(@mean,{ch1,ch2,ch3,ch4,ch5});
    

    即使每个 chX 数组的长度彼此不同,这也可以工作。

  • 您可以使用EVAL 生成单独的每个通道的变量平均值:

    对于 iChannel = 1:5
      varName = ['ch' int2str(iChannel)]; %# 创建名称字符串
      eval(['mean_'varName'=mean('varName');']);
    结尾
    

You have a few options for how you can do this:

  • You can put all your channel data into one large matrix first, then compute the mean of the rows or columns using the function MEAN. For example, if each chX variable is an N-by-1 array, you can do the following:

    chArray = [ch1 ch2 ch3 ch4 ch5];  %# Make an N-by-5 matrix
    meanArray = mean(chArray);        %# Take the mean of each column
    
  • You can put all your channel data into a cell array first, then compute the mean of each cell using the function CELLFUN:

    meanArray = cellfun(@mean,{ch1,ch2,ch3,ch4,ch5});
    

    This would work even if each chX array is a different length from one another.

  • You can use EVAL to generate the separate variables for each channel mean:

    for iChannel = 1:5
      varName = ['ch' int2str(iChannel)];  %# Create the name string
      eval(['mean_' varName ' = mean(' varName ');']);
    end
    
离去的眼神 2024-09-14 17:43:25

如果它总是恰好 5 个通道,你可以做

ch = {ch1, ch2, ch3, ch4, ch5}
for j = 1:5
    mean_ch(j) = mean(ch{j});
end

一个更复杂的方法是

for j = 1:nchannels
    mean_ch(j) = eval(['mean(ch' num2str(j) ')']);
end

If it's always exactly 5 channels, you can do

ch = {ch1, ch2, ch3, ch4, ch5}
for j = 1:5
    mean_ch(j) = mean(ch{j});
end

A more complicated way would be

for j = 1:nchannels
    mean_ch(j) = eval(['mean(ch' num2str(j) ')']);
end
手长情犹 2024-09-14 17:43:25

除了新手的回答。您可以使用结构和动态字段名称来完成您的任务。首先,我假设您的通道数据变量均采用 ch* 格式,并且是 MATLAB 工作区中唯一的变量。你可以做类似下面的事情

%# Move the channel data into a structure with fields ch1, ch2, ....
%# This could be done by saving and reloading the workspace
save('channelData.mat','ch*');
chanData = load('channelData.mat');

%# Next you can then loop through the structure calculating the mean for each channel
flds = fieldnames(chanData); %# get the fieldnames stored in the structure

for i=1:length(flds)
     mean_ch(i) = mean(chanData.(flds{i});
end

Apart from gnovice's answer. You could use structures and dynamic field names to accomplish your task. First I assume that your channel data variables are all in the format ch* and are the only variables in your MATLAB workspace. The you could do something like the following

%# Move the channel data into a structure with fields ch1, ch2, ....
%# This could be done by saving and reloading the workspace
save('channelData.mat','ch*');
chanData = load('channelData.mat');

%# Next you can then loop through the structure calculating the mean for each channel
flds = fieldnames(chanData); %# get the fieldnames stored in the structure

for i=1:length(flds)
     mean_ch(i) = mean(chanData.(flds{i});
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文