matlab找出稳定的值,在矩阵matlab中找到最接近的值

较小的情况下,可能会帮助你理解 -

代码

%%// Given big matrix, taken as a matrix of random numbers for demo

a1 = rand(10,5); %%// Replace this with your 300X200 matrix

%// For demo, let us assume, you are looking for the element that happens to be closest to the element in the 4th row and 5th column, which will be verified at the end

element = a1(4,5)+0.00001; %%// The element in search

%%// Find the linear index of the location

[~,ind] = min(reshape(abs(bsxfun(@minus,a1,element)),numel(a1),[]));

%%// Convert the linear index into row and column numbers

[x,y] = ind2sub(size(a1),ind)

输出

x =

4

y =

5

可以看出,产量预期的应答相匹配。

扩展部分:如果您有一组搜索数字,您可以使用bsxfun非常有效地处理它们的密切程度。这是如下图所示 -

代码

%%// Given big matrix, taken as a matrix of random numbers for demo

a1 = rand(10,5); %%// Replace this with your 300X200 matrix

%// For an array of search numbers

search_array = [a1(4,5)+0.00001;a1(6,5)+0.00001;a1(4,4)+0.00001;a1(4,2)+0.00001];

%%// Find the linear index of the location

[~,ind] = min(abs(bsxfun(@minus,a1(:),search_array')));%//'

%%// Convert the linear index into row and column numbers

[x,y] = ind2sub(size(a1),ind)

输出

x =

4 6 4 4

y =

5 5 4 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值