make vectors same length using min function

2 ビュー (過去 30 日間)
A-Rod
A-Rod 2024 年 6 月 17 日
コメント済み: dpb 2024 年 6 月 18 日
dear community let me ask your support.
my data:
x = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016];
y = [0, 0.05, 0.1, 0.15, 0.2];
a = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,2018,2019];
b = [0, 0.05, 0.1, 0.15, 0.2, 0.4];
I'm trying to make x & a vector lenght same as y & b vector length so Im using min fucntion
minlen = min(length(y), length(x));
minlen = min(length(b), length(a));
I'm trying to get x & a vector updated instead of getting this ans
x(1:minlen);
how can I make x & a equal to ans respectively? like this:
x = [2008, 2009, 2010, 2011, 2012];
a = [2008, 2009, 2010, 2011, 2012, 2013];
any feedback will be highly appreciated

採用された回答

Image Analyst
Image Analyst 2024 年 6 月 17 日
How about
x = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016];
y = [0, 0.05, 0.1, 0.15, 0.2];
a = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,2018,2019];
b = [0, 0.05, 0.1, 0.15, 0.2, 0.4];
x = x(1 : length(y))
x = 1x5
2008 2009 2010 2011 2012
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a = a(1 : length(b))
a = 1x6
2008 2009 2010 2011 2012 2013
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  4 件のコメント
Image Analyst
Image Analyst 2024 年 6 月 17 日
Yeah, I don't know the whole situation and just did what was asked for. It's possible that it might not work for other situations (such as y being shorter than x) and this is certainly not a robust solution for all possible caes. Originally I thought of using linspace to go from the min x to the max x
x = linspace(min(x), max(x), numel(y))
but then I saw he wanted it truncated instead of interpolated/rescaled, so then I just went with indexing to basically crop the x.
dpb
dpb 2024 年 6 月 18 日
I was just trying to forestall the followup Q? when the situation changed... :)

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2024 年 6 月 17 日
x = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016];
y = [0, 0.05, 0.1, 0.15, 0.2];
a = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,2018,2019];
b = [0, 0.05, 0.1, 0.15, 0.2, 0.4];
minlen = min(length(y), length(x));
You could use indexing and assignment:
xbackup = x; % for use in the next block of code
x = x(1:minlen) % Note the added "x = " to assign the indexing result back to x
x = 1x5
2008 2009 2010 2011 2012
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or if you're using a sufficiently recent release of MATLAB you could use trimdata.
x = xbackup % restoring the original x since the previous line overwrote it
x = 1x9
2008 2009 2010 2011 2012 2013 2014 2015 2016
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = trimdata(x, minlen)
x = 1x5
2008 2009 2010 2011 2012
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 件のコメント
A-Rod
A-Rod 2024 年 6 月 17 日
this alos works, thank you for sharing

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by