필터 지우기
필터 지우기

How can I merge two arrays in adjacent cells?.

조회 수: 7 (최근 30일)
Adarsh
Adarsh 2023년 11월 28일
편집: Dyuman Joshi 2023년 11월 28일
I have two different vectors which contain many x-coordinate and y-coordinate in them. I want to merge them in one array where first cells will have x1 and y1-coordinate, second cell will have x2 and y2-coordinate and etc.. I have written a code but it gives different results. It just joins the y coordinates at the point where x-coordinates end.
c_start_x = x_vertices(1:2:end);
c_end_x = x_vertices(2:2:end);
c_start_y = y_vertices(1:2:end);
c_end_y = y_vertices(2:2:end);
c_start = [transpose(c_start_x);transpose(c_start_y)];

답변 (2개)

Dyuman Joshi
Dyuman Joshi 2023년 11월 28일
I assume that the x and y data is stored in arrays x_vertices and y_vertices respectively.
C = [x_vertices(:) y_vertices(:)]
Here the data will be arranged row-wise, i.e. each coordinate pair will be in the corresponding row.
  댓글 수: 2
Adarsh
Adarsh 2023년 11월 28일
Hello , thank you for this answer , but this will give me a Nx2 matrix . However, what i want is Nx1 where both x and y are stored in one cell.
Ex - X = [1, 2, 3] and Y = [4, 5, 6]
I want
Z = ([1;4], [2;5], [3;6])
Dyuman Joshi
Dyuman Joshi 2023년 11월 28일
편집: Dyuman Joshi 2023년 11월 28일
Ok, so you want a cell array. In that case, one approach is -
X = [1 2 3];
Y = [4 5 6];
Z = mat2cell([X;Y], 2, ones(1, numel(X)));
Z{1}
ans = 2×1
1 4
Though, Is there any particular reason why you want to store the data in a cell array?
Generally, storing in a numerical array is quite efficient than storing in a cell array.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2023년 11월 28일
X = [1,2,3];
Y = [4,5,6];
C = arrayfun(@horzcat,X,Y, 'uni',0)
C = 1×3 cell array
{[1 4]} {[2 5]} {[3 6]}

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by