How to get data from 2D array and put into 1D array

后端 未结 3 397
眼角桃花
眼角桃花 2021-01-27 14:44

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.

3条回答
  •  难免孤独
    2021-01-27 15:40

    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
    

提交回复
热议问题