how to reduce the size of array as small as the smallest array to have them in one matrix
13 ビュー (過去 30 日間)
表示 古いコメント
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
Thanks in advance
3 件のコメント
the cyclist
2023 年 2 月 26 日
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?
回答 (1 件)
Jan
2023 年 2 月 26 日
編集済み: Jan
2023 年 2 月 26 日
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
nz = numel(z);
b1 = [x(1:nz); ...
y(1:nz);
z]
b2 = [x(numel(x) - nz + 1:numel(x)); ...
y(numel(y) - nz + 1:numel(y)); ...
z]
c1 = [x; ...
interp1(1:numel(y), y, linspace(1, numel(y), numel(x))); ...
interp1(1:numel(z), z, linspace(1, numel(z), numel(x)))]
c2 = [interp1(1:numel(x), x, linspace(1, numel(x), nz)); ...
interp1(1:numel(y), y, linspace(1, numel(y), nz)); ...
z]
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))]
4 件のコメント
Image Analyst
2023 年 2 月 27 日
@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.
参考
カテゴリ
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!