gpt4 book ai didi

matlab - 同时删除矩阵中的一行和一列

转载 作者:行者123 更新时间:2023-12-02 15:17:18 24 4
gpt4 key购买 nike

是否可以同时删除矩阵的一行和一列?

示例:

M = magic(3)

M =

8 1 6
3 5 7
4 9 2

然后使用单行删除第 2 列和第 2 行以获得:

M = 

8 6
4 2

最佳答案

您可以为行和列索引提供一个索引数组,MATLAB 将自动返回所有排列(通常是一个烦人的功能,但在这里是一个好处)。

所以你可以这样做:

M([1 3], [1 3])

或者更灵活,你可以使用逻辑索引

M([true false true], [true false true])

或更一般地(可能有更多的行和列)

row = 2;        % Could be an array of rows to exclude
column = 2; % Could be an array of columns to exclude

out = M(~ismember(1:size(M, 1), row), ~ismember(1:size(M, 2), column))

关于matlab - 同时删除矩阵中的一行和一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39494726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com