Combining arrays with different dimensions

조회 수: 4 (최근 30일)
Ruhul Islam
Ruhul Islam 2022년 3월 31일
답변: MJFcoNaN 2022년 4월 4일
how do i combine multiple arrays with different dimensions into a single one
For example:
array_1 = [1.1, 1.25, 1.3, 1.5, 1.6, 1.9, 1.99; 100, 120, 50, 78, 12, 15, 22]
array_2 = [1.09, 1.12, 1.21, 1.46, 1.58, 1.6, 1.89, 1.95, 1.99; 10, 130, 20, 12, 77, 34, 5, 22, 97]
The first row for each array can be seen as x values and seccond row will be y values, therefore the code should produce the following data
combined = [1.09, 1.1, 1.12, 1.21, 1.25, 1.3, 1.46, 1.5, 1.58, 1.6, 1.89, 1.9, 1.95, 1.99; 0, 100, 0, 0, 120, 50, 0, 78, 0, 12, 0, 15, 0, 22; 10, 0, 130, 20, 0, 0, 12, 0, 77, 34, 5, 0, 22, 92]
  댓글 수: 1
Torsten
Torsten 2022년 3월 31일
The rule how you combine the two matrices (i.e. the indices when you take one and when you take two entries one after the other from array_2) is not clear to me.

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

답변 (1개)

MJFcoNaN
MJFcoNaN 2022년 4월 4일
I guess the last "92" is a typo.
x1 = array_1(1, :);
x2 = array_2(1, :);
y1 = array_1(2, :);
y2 = array_2(2, :);
x = unique([x1, x2]);
y = zeros(2, length(x));
[~, locb1] = ismember(x1, x);
[~, locb2] = ismember(x2, x);
y(1, locb1) = y1;
y(2, locb2) = y2;

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by