如何从二维数组中获取数据并放入一维数组中 [英] How to get data from 2D array and put into 1D array

查看:212
本文介绍了如何从二维数组中获取数据并放入一维数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a 2D array and I want to create a 1D by MATLAB, satisfying the requirement that each element of the 1D output was created by the value of a given index into the 2D array. Example 2D array is

A=[2 4 6; 1 9 7.3 4 5]

1D阵列的索引

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

我想存储1D数组,其元素由

I want to store the 1D array with elements determined by

 B=A(x,y) % x,y are index in X and Y matrix

示例构建一维数组:

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

怎么能我实现了吗?
全部谢谢。

How can I implement it? Thanks all.

推荐答案

您正在寻找 sub2ind

You are looking for sub2ind:

A=[2 4 6; 1 9 7; 3 4 5]
X=[1;2;3]; Y=[1;2;3];
B = A(sub2ind(size(A),X,Y))

B =

     2
     9
     5

这篇关于如何从二维数组中获取数据并放入一维数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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