# how to reduce the size of array as small as the smallest array to have them in one matrix

44 visualizzazioni (ultimi 30 giorni)
arash rad il 26 Feb 2023
Commentato: Image Analyst il 27 Feb 2023
Hello everyone
I have three arrays and size of each is x 1*104 , y is 1*100 and z is 1*95 and I Have them in a matrix like : T = [x ; y ;z]
How I reduce the size of y and x and make them as large as z to not have inconsistent error
##### 3 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
the cyclist il 26 Feb 2023
Considering a specific, smaller version of your problem, suppose your inputs are
x = [2 3 5 7 11]; % length 5
y = [13 17 19] % length 3
z = [23 29]; % length 2
What would you want the output to be?

Accedi per commentare.

### Risposte (1)

Jan il 26 Feb 2023
Modificato: Jan il 26 Feb 2023
There are several possibilities:
• Fill the shorter arrays with zeros or NaNs on the top, bottom or both.
• Crop the longer arrays at the start or end.
• Interpolate two vectors to have the same size as the 3rd one.
• Interpolate all vectors to a greater or smaller number of elements.
With the shorter example of the cyclist:
x = [2 3 5 7 11]; % length 5
y = [13 17 19]; % length 3
z = [23 29]; % length 2
a = zeros(3, 5); % Or nan(3, 5)
a(1, :) = x;
a(2, 1:numel(y)) = y;
a(3, 1:numel(z)) = z
a = 3×5
2 3 5 7 11 13 17 19 0 0 23 29 0 0 0
nz = numel(z);
b1 = [x(1:nz); ...
y(1:nz);
z]
b1 = 3×2
2 3 13 17 23 29
b2 = [x(numel(x) - nz + 1:numel(x)); ...
y(numel(y) - nz + 1:numel(y)); ...
z]
b2 = 3×2
7 11 17 19 23 29
c1 = [x; ...
interp1(1:numel(y), y, linspace(1, numel(y), numel(x))); ...
interp1(1:numel(z), z, linspace(1, numel(z), numel(x)))]
c1 = 3×5
2.0000 3.0000 5.0000 7.0000 11.0000 13.0000 15.0000 17.0000 18.0000 19.0000 23.0000 24.5000 26.0000 27.5000 29.0000
c2 = [interp1(1:numel(x), x, linspace(1, numel(x), nz)); ...
interp1(1:numel(y), y, linspace(1, numel(y), nz)); ...
z]
c2 = 3×2
2 11 13 19 23 29
c3 = [interp1(1:numel(x), x, linspace(1, numel(x), 10)); ...
interp1(1:numel(y), y, linspace(1, numel(y), 10)); ...
interp1(1:numel(z), z, linspace(1, numel(z), 10))]
c3 = 3×10
2.0000 2.4444 2.8889 3.6667 4.5556 5.4444 6.3333 7.4444 9.2222 11.0000 13.0000 13.8889 14.7778 15.6667 16.5556 17.2222 17.6667 18.1111 18.5556 19.0000 23.0000 23.6667 24.3333 25.0000 25.6667 26.3333 27.0000 27.6667 28.3333 29.0000
##### 4 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Image Analyst il 27 Feb 2023
@arash rad OK, so you just wanted to crop off any part of the vectors that are beyond the length of Z. It would have eliminated a lot of confusion if you had just explained that in the very initial post.

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by