matlab - Matlab-如何在矩阵中找到数字的相邻元素?
问题描述
例如,如果我有一个矩阵:
A=[1 2 3 4; 5 6 7 8; 9 10 11 12]
如果我选择数字 6,那么它应该返回一个矩阵:
B=[1 2 3; 5 6 7; 9 10 11]
解决方案
干得好:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
num=6;
[i,j]=find(A==num);
[len_row,len_col]=size(A);
cols=(j-1):(j+1);
cols(cols<1)=[]; %to avoid trying to access values outside matrix
cols(cols>len_col); %to avoid trying to access values outside matrix
row=(i-1):(i+1);
row(row<1)=[]; %to avoid trying to access values outside matrix
row(row>len_row)=[]; %to avoid trying to access values outside matrix
new_mat=A(row,cols);
推荐阅读
- reactjs - 在reactjs中单击上传文件时无法显示选择文件的弹出窗口?
- r - 如何比较两个独立组的两个相关值?
- javascript - 如何在javascript中获取两个值或节点之间的差异
- javascript - autoML 无法使用 google cli 命令下载模型
- julia - 从匿名函数访问另一个函数的参数
- python - Python正则表达式用一个或多个单词验证名称?
- ruby-on-rails - 在相同的 2 个对象之间有 2 个不同的关联通常是不好的做法吗?
- algorithm - 算法 - 最小交换以调整矩阵
- python - 我正在使用 pygame 开发这款游戏,我正在尝试添加动画电源,但我遇到了麻烦
- ios - 在 Flutter VScode 中运行调试 ios 应用程序时出错