如何使用 if 语句或使用任何其他合适的方法从 for 循环创建子图

How can I create a subplot from for loop with an if statement or using any other suitable method

我有两个阈值(标量),我想通过我的 for 循环 运行,然后将每个 'meanMUA'(针对该特定阈值)绘制为子图。

我尝试了以下操作,但是 threshold1 的 'meanMUA' 在两个指定位置进行了子图,而不是 threshold1(在位置 1)和 threshold2(在位置 2)的 'meanMUA':

threshold1 = 0.018; 
threshold2 = 2*threshold1;

figure;
for threshold = 1:2
    
    for trial = 1:50

        for channel = 1:16

            for bin = 1:10000

                %%access data from bins, using sliding window
                bindata = absData(((bin*binWidth)+1-binWidth : bin*binWidth), channel, trial);

                if threshold == threshold1 || threshold == threshold2

                    %%calculate mua above threshold
                    mua(bin, channel, trial) = sum(bindata >= threshold);

                end

            end

        end

    end
    
meanMUA = (mean(mua,3)).';

subplot(1,2, threshold);
clims=[0 1];
imagesc(meanMUA,clims)
 
end

我尝试了此代码的其他变体,包括重新构造 if/elseif 语句并计算 if/elseif 语句中两个阈值的 meanMUA,尽管这也不起作用:

if threshold == threshold1

        %%calculate mua above threshold
        mua(bin, channel, trial) = sum(bindata >= threshold);
        meanMUA = (mean(mua,3)).';
        
        subplot(1,2, threshold);
        clims=[0 1];
        imagesc(meanMUA,clims)

     elseif threshold == threshold2

        %%calculate mua above threshold
        mua(bin, channel, trial) = sum(bindata >= threshold);
        meanMUA = (mean(mua,3)).';
        
        subplot(1,2, threshold);
        clims=[0 1];
        imagesc(meanMUA,clims)
        
end

有谁知道我该如何解决这个问题?感谢您的帮助。

我正在使用 Matlab 2019b。

提前致谢。

在第一个for循环中,for threshold = 1:2表示threshold只能取值1和2,永远不会等于threshold1 = 0.018threshold2 = 2*threshold1 = 0.036

一种解决方案是在向量中定义两个阈值:thresholds = [0.018, 2*0.018]。然后在for循环中:

for t = 1:2
    ...
    %%access data from bins, using sliding window
    bindata = absData(((bin*binWidth)+1-binWidth : bin*binWidth), channel, trial)

    %%calculate mua above threshold
    mua(bin, channel, trial) = sum(bindata >= thresholds(t));
    ...
end

这将提供具有阈值 threshold1 和 threshold2 的 bindata 的值。