Combining arrays with different dimensions

3 views (last 30 days)
Ruhul Islam
Ruhul Islam on 31 Mar 2022
Answered: MJFcoNaN on 4 Apr 2022
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 Comment
Torsten
Torsten on 31 Mar 2022
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.

Sign in to comment.

Answers (1)

MJFcoNaN
MJFcoNaN on 4 Apr 2022
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;

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!