Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matlab中一行循环的矢量化嵌套_Matlab_Matrix_Vectorization - Fatal编程技术网

matlab中一行循环的矢量化嵌套

matlab中一行循环的矢量化嵌套,matlab,matrix,vectorization,Matlab,Matrix,Vectorization,我清楚地记得一个专家代码检查I,j上的某些条件,如果评估为真,他们会在矩阵中标记。下面显示的行中的某些内容。他们一行就做到了!!!有人能告诉我怎么做吗?在Matlab中编写以下代码最有效的方法是什么 for i=1:nrows for j=1:ncolumns if (3*i+4*j>=2 && 6*i-j<=6) obstacle(i,j)=1; end end end 对于i=1:nrows

我清楚地记得一个专家代码检查
I
j
上的某些条件,如果评估为真,他们会在矩阵中标记。下面显示的行中的某些内容。他们一行就做到了!!!有人能告诉我怎么做吗?在Matlab中编写以下代码最有效的方法是什么

for i=1:nrows
    for j=1:ncolumns
        if (3*i+4*j>=2 && 6*i-j<=6)
            obstacle(i,j)=1;
        end
    end
end
对于i=1:nrows
对于j=1:n列
如果(3*i+4*j>=2&&6*i-j,您可以在此处使用,并借助来处理类似的复杂条件语句-

%// Define vectors instead of the scalar iterators used in original code
ii=1:nrows
jj=1:ncolumns

%// Look for logical masks to satisfy all partial conditional statements
condition1 = bsxfun(@plus,3*ii',4*jj)>=2  %//'
condition2 = bsxfun(@plus,6*ii',-1*jj)<=6 %//'

%// Form the complete conditional statement matching logical array
all_conditions = condition1 & condition2

%// Use logical indexing to set them to the prescribed scalar
obstacle(all_conditions) = 1

下次您提问时,请提供所有详细信息,以避免根据提供的答案编辑问题。谢谢!感谢您的解释。@giffy绝对是我的荣幸!添加了一个旁注,可能对您也很有趣!如果我有一个圆条件,
I*I+j*j@giffy使用
ndgrid
方法并使用
o障碍物(I.*I+J.*J)
%// Form the 2D matrix of iterators
[I,J] = ndgrid(1:nrows,1:ncolumns)

%// Form the 2D conditional array and use logical indexing to set all those
obstacle(3*I+4*I>=2 & 6*I-J<=6) = 1