Matlab 如何从二维数组中获取数据并放入一维数组

Matlab 如何从二维数组中获取数据并放入一维数组,matlab,matrix,indexing,Matlab,Matrix,Indexing,我有一个2D数组,我想用MATLAB创建一个一维数组,满足一维输出的每个元素都是由2D数组中给定索引的值创建的要求。示例2D数组是 A=[2 4 6; 1 9 7.3 4 5] 和一维数组的索引 X=[1;2;3] Y=[1;2;3] 我想存储1D数组,其中的元素由 B=A(x,y) % x,y are index in X and Y matrix 构建1D阵列的示例: X=[1;2;3] Y=[1;2;3] B=[A(1,1);A(2,2);A(3,3)]=[2; 9; 5]

我有一个2D数组,我想用MATLAB创建一个一维数组,满足一维输出的每个元素都是由2D数组中给定索引的值创建的要求。示例2D数组是

A=[2 4 6; 1 9 7.3 4 5]
和一维数组的索引

X=[1;2;3]
Y=[1;2;3]
我想存储1D数组,其中的元素由

 B=A(x,y) % x,y are index in X and Y matrix
构建1D阵列的示例:

X=[1;2;3]
Y=[1;2;3]

 B=[A(1,1);A(2,2);A(3,3)]=[2; 9; 5]
这是我的密码

B=zeros(1,length(A));
B=A(...)  %I don't know it
我如何实现它?
谢谢大家。

你可以使用
cellfun
来做这件事。按列将单元格转换为单元格,并对单元格的每个元素执行
f

 A=[2 4 6; 1 2 7];

 % some example f funcion that just adds the col_index_A and row_index_A
 f = @(col_index_A, row_index_A) col_index_A + row_index_A; 

 % execute f with parameters that come from each column of A
 B = cellfun(@(c) f(c(1), c(2)), num2cell(A, 1));

 B =

     3     6    13

您可以使用
cellfun
来执行此操作。按列将单元格转换为单元格,并对单元格的每个元素执行
f

 A=[2 4 6; 1 2 7];

 % some example f funcion that just adds the col_index_A and row_index_A
 f = @(col_index_A, row_index_A) col_index_A + row_index_A; 

 % execute f with parameters that come from each column of A
 B = cellfun(@(c) f(c(1), c(2)), num2cell(A, 1));

 B =

     3     6    13

您可以使用
cellfun
来执行此操作。按列将单元格转换为单元格,并对单元格的每个元素执行
f

 A=[2 4 6; 1 2 7];

 % some example f funcion that just adds the col_index_A and row_index_A
 f = @(col_index_A, row_index_A) col_index_A + row_index_A; 

 % execute f with parameters that come from each column of A
 B = cellfun(@(c) f(c(1), c(2)), num2cell(A, 1));

 B =

     3     6    13

您可以使用
cellfun
来执行此操作。按列将单元格转换为单元格,并对单元格的每个元素执行
f

 A=[2 4 6; 1 2 7];

 % some example f funcion that just adds the col_index_A and row_index_A
 f = @(col_index_A, row_index_A) col_index_A + row_index_A; 

 % execute f with parameters that come from each column of A
 B = cellfun(@(c) f(c(1), c(2)), num2cell(A, 1));

 B =

     3     6    13

我不确定我是否理解您的问题,但我认为您希望在2×n矩阵上应用函数

试一试


我不确定我是否理解您的问题,但我认为您希望在2×n矩阵上应用函数

试一试


我不确定我是否理解您的问题,但我认为您希望在2×n矩阵上应用函数

试一试


我不确定我是否理解您的问题,但我认为您希望在2×n矩阵上应用函数

试一试

您正在寻找:

您正在寻找:

您正在寻找:

您正在寻找:


@用户3051460没问题!如果这是您想要的,请接受。:)@用户3051460没问题!如果这是您想要的,请接受。:)@用户3051460没问题!如果这是您想要的,请接受。:)@用户3051460没问题!如果这是您想要的,请接受。:)