示例(为了与其它元素区别,赋值用NaN):
% 生成测试数据B = magic(5)
B = B(1:3, :)
[r,c] = find(B>20)
A = [r c]
% 将B中以A作为下标的元素置为NaN
B((A(:,2)-1)*size(B,1)+A(:,1)) = NaN
输出:
>>B = magic(5)
>>B = B(1:3, :)
B =
1724 1 815
23 5 71416
4 6132022
>>[r,c] = find(B>20)
>>A = [r c]
A =
2 1
1 2
3 5
>>B((A(:,2)-1)*size(B,1)+A(:,1)) = NaN
B =
17 NaN 1 815
NaN 5 71416
4 61320 NaN
附:提取矩阵御培元素的三种方法:
diag(B(A(:,1),A(:,2)))
arrayfun(@(r,c)B(r,c),A(:,1),A(:,2))
B((A(:,2)-1)*size(B,1)+A(:,1))
n = 10 % 随机点数量rgx = [-10 10] % x坐标范围
rgy = [-8 8] % y坐标范围
A = []
for p 耐乱猜= 1:n
x = (max(rgx)-min(rgx))*rand(n,1)+min(rgx)
y = 陪派(max(rgy)-min(rgy))*rand(n,1)+min(rgy)
A = 昌型[A x y]
end
s为索引矩阵,m为横坐标,n为纵坐标腊宴
如果使用以没局锋下方法,肯定得不到想要的结果,结果是一个方阵
>>s1=s(m,n)
可以使用枯晌循环实现:
>>s1=zeros(1,length(m))
for i=1:length(m)
s1(i)=[s(m(i),n(i))]
end
s1
s1 =
8 12 15 20 24 28 31
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)