【问题标题】:intermittent loops in matlabmatlab中的间歇循环
【发布时间】:2013-03-21 10:21:38
【问题描述】:

你好再次合乎逻辑的朋友! 我知道这是一个相当复杂的问题,所以请多多包涵!我想我已经设法把它归结为两个细节:- 我需要两个我似乎无法工作的循环...... 首先;变量rollers(1).ink 是一个包含墨水值的(12x1) 向量。该程序在每个连接处的辊子之间平均分配墨水。我试图让rollers(1).ink 仅在特定时间步与rollers(2) 交互。每转一圈,墨水应转移到系统一次,即 nTimesSteps = nBins_max 的每个倍数。当系统旋转时,墨水不应转移回滚轮 (1).ink - 每转一次,它应该只将墨水引入系统一次,而不会带回任何墨水。目前我设置了rollers(1).ink = one,但仅用于测试。我真的被困在这里了! 第二;它需要这样做的原因是因为在模拟结束时我还希望以打印图像的形式去除墨水。图像应该是我系统中最后一个滚筒上墨水的反映,并且该值的一半应该从最后一个滚筒上移除,并在每次旋转时从系统中取出。最后一个滚筒上剩余的墨水应回收并在系统中“重新分流”,为下一次旋转做好准备。 所以……我认为它是在循环开始的第 86 行,我需要做所有这些事情。在伪中,对于间歇性的馈送,我一直在尝试类似:

For k = 1:nTimeSteps
While nTimesSteps = mod(nTimeSteps, nBins_max) == 0 % This should only output when nTimeSteps is a whole multiple of nBins_max i.e. one full revolution
‘Give me the ink on each segment at each time step in a matrix’
End

averageAmountOfInk 的输出是我想要返回此数据的确切格式,但我并不真正需要平均值,只需要每个时刻的实际值。当我尝试使用以下方法重新创建它时,我不断收到尺寸不匹配的错误:

For m = 1:nTimeSteps
    For n = 1:N
Rollers(m,n) = rollers(n).ink’;
    End
End

如果有人有兴趣了解它目前的作用,我将在下面发布完整的代码。最后还有一个函数当然需要保存到单独的文件中。 我已经多次发布了这个问题的变体,但我完全意识到这是一个相当棘手的问题,而且我发现很难通过互联网传达我的意图! 如果有人对我缺乏编程技能有任何想法/建议/一般侮辱,请随时回复!

%% Simple roller train
% # Single forme roller
% # Ink film thickness = 1 micron

clc
clear all
clf
% # Initial state
C = [0,70;   % # Roller centres (x, y)
     10,70;
     21,61;
     11,48;
     21,34;
     27,16;
     0,0
     ];
R = [5.6,4.42,9.8,6.65,10.59,8.4,23];    % # Roller radii (r)
% # Direction of rotation (clockwise = -1, anticlockwise = 1)
rotDir = [1,-1,1,-1,1,-1,1]';
 N = numel(R);           % # Amount of rollers

% # Find connected rollers
isconn = @(m, n)(sum(([1, -1] * C([m, n], :)).^2)...
    -sum(R([m, n])).^2 < eps);
[Y, X] = meshgrid(1:N, 1:N);
conn = reshape(arrayfun(isconn, X(:), Y(:)), N, N) - eye(N);

% # Number of bins for biggest roller
nBins_max = 50;
nBins = round(nBins_max*R/max(R))';

% # Initialize roller struct
rollers = struct('position',{}','ink',{}','connections',{}',...
    'rotDirection',{}');

% # Initialise matrices for roller properties
for ii = 1:N
    rollers(ii).ink = zeros(1,nBins(ii));
    rollers(ii).rotDirection = rotDir(ii);
    rollers(ii).connections = zeros(1,nBins(ii));
    rollers(ii).position = 1:nBins(ii);
end

for ii = 1:N
    for jj = 1:N
        if(ii~=jj)
            if(conn(ii,jj) == 1)
               connInd = getConnectionIndex(C,ii,jj,nBins(ii));
               rollers(ii).connections(connInd) = jj;
            end
        end
    end
end

% # Initialize averageAmountOfInk and calculate initial distribution
nTimeSteps = 1*nBins_max;
averageAmountOfInk = zeros(nTimeSteps,N);
inkPerSeg = zeros(nTimeSteps,N);
for ii = 1:N
    averageAmountOfInk(1,ii) = mean(rollers(ii).ink);
end

% # Iterate through timesteps
for tt = 1:nTimeSteps
        rollers(1).ink = ones(1,nBins(1));

        % # Rotate all rollers
    for ii = 1:N
                rollers(ii).ink(:) = ...
                circshift(rollers(ii).ink(:),rollers(ii).rotDirection);
    end

% # Update all roller-connections
for ii = 1:N
    for jj = 1:nBins(ii)
        if(rollers(ii).connections(jj) ~= 0)
            index1 = rollers(ii).connections(jj);
            index2 = find(ii == rollers(index1).connections);
            ink1 = rollers(ii).ink(jj);
            ink2 = rollers(index1).ink(index2);
            rollers(ii).ink(jj) = (ink1+ink2)/2;
            rollers(index1).ink(index2) = (ink1+ink2)/2;
        end
     end
end

% # Calculate average amount of ink on each roller
    for ii = 1:N
        averageAmountOfInk(tt,ii) = sum(rollers(ii).ink);
    end
end

    image(5:20) = (rollers(7).ink(5:20))./2;
    inkPerSeg1 = [rollers(1).ink]';
    inkPerSeg2 = [rollers(2).ink]';
    inkPerSeg3 = [rollers(3).ink]';
    inkPerSeg4 = [rollers(4).ink]';
    inkPerSeg5 = [rollers(5).ink]';
    inkPerSeg6 = [rollers(6).ink]';
    inkPerSeg7 = [rollers(7).ink]';

【问题讨论】:

    标签: matlab matrix simulation


    【解决方案1】:

    这是一个扩展评论而不是一个正确的答案,但是评论框有点太小了......

    你的代码让我不知所措,我只见树木不见森林。我建议您消除所有我们不需要看到的东西来帮助您解决眼前的问题(例如所有那些画线图)——我认为这将帮助您自己调试代码以将所有这些东西放入函数或脚本。

    你的代码 sn-p

    For k = 1:nTimeSteps
        While nTimesSteps = mod(nTimeSteps, nBins_max) == 0 
        ‘Give me the ink on each segment at each time step in a matrix’
    End
    

    可能是(我不太明白你对while 语句的使用,While 这个词不是 Matlab 关键字,正如你所写的那样,语句返回的值不会随着迭代而改变到迭代)等价于

    For k = 1:nBins_max:nTimeSteps
        ‘Give me the ink on each segment at each time step in a matrix’
    End
    

    您似乎错过了 Matlab 冒号运算符的一个基本功能......

    1:8 = [1 2 3 4 5 6 7 8]
    

    但是

    1:2:8 = [1 3 5 7]
    

    也就是说,三元组中的第二个数字是连续元素之间的步幅。

    您的矩阵conn 在连接滚轮的(row,col) 处为1,在其他位置为0。您可以像这样找到所有1s 的行和列索引:

    [ri,ci] = find(conn==1)
    

    然后您可以在没有循环嵌套和开始的 if 语句的情况下获取 1s 的 (row,col) 位置

    for ii = 1:N
        for jj = 1:N
            if(ii~=jj)
                if(conn(ii,jj) == 1)
    

    我可以继续,但不会,这足以发表一条评论。

    【讨论】:

    • 感谢您的建议。我知道 (:) 运算符,实际上我曾尝试完全按照您在此处建议的形式使用它,但没有成功(可能在以前的帖子中)。我认为它只会实现我想要做的一半,即 - 如果它不引入墨水,我需要一些方法让系统忽略这个滚筒。我感谢您的兴趣和您提出的建议,特别是简单性'find',一旦代码工作,我会花时间精简代码。我已经按照建议编辑了这个问题 - 希望在树上创造一个小空地!谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多