根据行号和列号在数组中查找值 [英] find value in array according to the row and column number

查看:90
本文介绍了根据行号和列号在数组中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个矩阵A和B,矩阵A(1,:)= [1 2]的第一行指的是行和列矩阵B(1,2)= 21的数量,现在我想做这项工作适用于矩阵A的另一行而没有循环吗?

I have two matrix A and B,the first row of matrix A(1,:)=[1 2] refer to the number of row and column matrix B(1,2)=21,now I want to do this work for another rows of matrix A without loops?

A=[1 2;2 3;1 3;3 3];
B=[1 21 34;45 65 87;4 55 66];
for i=1:4
d(i,:)=B(A(i,1),A(i,2))
end
d =[21; 87;34;66] 

推荐答案

sub2ind的替代方案是

d = B(A(:,1)+ (A(:,2)-1)*size(B,1));

这篇关于根据行号和列号在数组中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆