matlab分块矩阵取某一块,如何在Matlab中使用条件提取矩阵的一部分

I have a sat of matrices and I want to extract only a part of the matrix that satisfy a condition.

For example: values of the 150x180 matrix goes from 0 to 2.80 and I only want those between 1.66 and 1.77

I want to keep the values within the rang in their original location in the original matrix and set the other to zero.

can anybody help me please.

Thank you

解决方案

You can use logical indexing. First, find A entries that do not satisfy your conditions. Next, using A(idx) change them to 0:

% example matrix

A = 2.8*rand(150, 180);

% find entries meeting some criterion

idx = A<1.66 | A>1.77;

A(idx) = 0;

Or simpler, as Rody Oldenhuis suggested, you can include the logical expression directly in the matrix reference:

A(A<1.66 | A>1.77) = 0;

This yields a shorter and cleaner code, but not a faster code: MATLAB still explicitly creates the logical index variable, but clears it afterwards.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值