Matlab与C++;求矩阵中等于某物的第一个元素 为什么在C++中使用这种算法在Matlab中不工作? int a[3][4] = { {0, 1, 1, 1} , /* initializers for row indexed by 0 */ {1, 0, 0, 0} , /* initializers for row indexed by 1 */ {0, 0, 0, 1} /* initializers for row indexed by 2 */ }; int x,y; for(int i=1;i<3;i++) for(int j=1;j<4;j++) { if (a[i][j]==1) // if element is equal to 1, get his position and stop. { x=i-1; y=j-1; break; } } cout<<x<<" "<<y; // 1 2

Matlab与C++;求矩阵中等于某物的第一个元素 为什么在C++中使用这种算法在Matlab中不工作? int a[3][4] = { {0, 1, 1, 1} , /* initializers for row indexed by 0 */ {1, 0, 0, 0} , /* initializers for row indexed by 1 */ {0, 0, 0, 1} /* initializers for row indexed by 2 */ }; int x,y; for(int i=1;i<3;i++) for(int j=1;j<4;j++) { if (a[i][j]==1) // if element is equal to 1, get his position and stop. { x=i-1; y=j-1; break; } } cout<<x<<" "<<y; // 1 2,c++,matlab,matrix,C++,Matlab,Matrix,我该怎么做呢?这并不是说中断被忽略,而是它只从内部循环中断,而只是在外部循环中继续。恐怕没有“休息两次”或类似的指示。一种解决方案是在外部循环中也设置一个中断标志: int x, y; bool stop = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if (a[i][j] == 1) // if element is equal to 1, get his p

我该怎么做呢?

这并不是说
中断被忽略,而是它只从内部循环中断,而只是在外部循环中继续。恐怕没有“休息两次”或类似的指示。一种解决方案是在外部循环中也设置一个中断标志:

int x, y;
bool stop = false;
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 4; j++)
    {
        if (a[i][j] == 1) // if element is equal to 1, get his position and stop.
        {
            x = i - 1;
            y = j - 1;
            stop = true;
            break;
        }
    }
    if (stop)
        break;
}
其他解决方案包括将嵌套循环放入其自己的函数中,并使用
return

#include <iostream>
#include <tuple>
std::tuple<int, int> getXY() {
    int a[3][4] = {
       {0, 1, 1, 1} ,   /*  initializers for row indexed by 0 */
       {1, 0, 0, 0} ,   /*  initializers for row indexed by 1 */
       {0, 0, 0, 1}   /*  initializers for row indexed by 2 */
    };
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            if (a[i][j] == 1) // if element is equal to 1, get his position and stop.
                return std::tuple<int, int>{i-1, j-1};
        }
    }
}

int main() {
    auto[x, y] = getXY();
    std::cout << x << " " << y;
}
#包括
#包括
std::tuple getXY(){
INTA[3][4]={
索引为0的行的{0,1,1,1},/*初始值设定项*/
由1索引的行的{1,0,0,0},/*初始值设定项*/
由2索引的行的{0,0,0,1}/*初始值设定项*/
};
对于(int i=0;i<3;i++)
{
对于(int j=0;j<4;j++)
{
若(a[i][j]==1)//若元素等于1,则获取其位置并停止。
返回std::tuple{i-1,j-1};
}
}
}
int main(){
auto[x,y]=getXY();
标准::cout
  • 这是在MATLAB中正确使用
    break
    的问题。从中,您将看到以下语句:
在嵌套循环中,break仅从发生它的循环中退出。 控件传递给该循环结束后的语句

因此,使用MATLAB,您可以按以下方式使用
break

stopFlag = 0
for i=1:m
    for j=1:n
        if a(i,j)==1
            k=i-1;
            l=j-1;
            stopFlag = 1;
            break;
        end
    end
    if stopFlag
      break;
    end
end
  • 如果您想在MATLAB中以更高效的方式执行此操作,可以尝试以下代码:

1、中断只会中断内部for循环,因此不是第一个出现,而是第一个出现在最后一行中。2。C++标引开始于0,Matlab标引为1,因此<代码> x= I-1;和 y= J-1; >不等于<代码> k= I-1; L= J-1;。我不喜欢使用<代码> STD::tuple < /C> >,这里是这样的。重型和过度使用…@user1810087是的,人们经常不恰当地使用它。但我认为在这种情况下,返回两个值是最不可怕的方式。你有什么建议吗?@user1810087据我所知,从函数返回多个值是推送tuple的主要功能之一for@JHBonarius事实上,这就是问题所在直到OP(不正确)更改为止it@Blaze
if(stop)break;
应在内部循环之后插入,而不是在开始时立即中断整个循环。
#include <iostream>
#include <tuple>
std::tuple<int, int> getXY() {
    int a[3][4] = {
       {0, 1, 1, 1} ,   /*  initializers for row indexed by 0 */
       {1, 0, 0, 0} ,   /*  initializers for row indexed by 1 */
       {0, 0, 0, 1}   /*  initializers for row indexed by 2 */
    };
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            if (a[i][j] == 1) // if element is equal to 1, get his position and stop.
                return std::tuple<int, int>{i-1, j-1};
        }
    }
}

int main() {
    auto[x, y] = getXY();
    std::cout << x << " " << y;
}
stopFlag = 0
for i=1:m
    for j=1:n
        if a(i,j)==1
            k=i-1;
            l=j-1;
            stopFlag = 1;
            break;
        end
    end
    if stopFlag
      break;
    end
end
[r,c] = find(a'==1)
k = r(1)-1;
l = c(1)-1;