How to append arrays of different lengths

30 ビュー (過去 30 日間)
Yash
Yash 2022 年 7 月 1 日
回答済み: GeeTwo 2022 年 7 月 1 日
I am trying to extract values from a structure, but those arrays are of different lengths. I was wondering how I could make a structure/array with those arrays of different lengths. I either want to try creating a padding for the arrays or create a structure with the different lengths within it.
Below is my attempt of trying to create the newarr by appending newarray values to it, but a horzcat error prevents appending once the length of the array changes from 8882*2 to 8879*2.
newarr=[];
for n=1:size((dist_arr),1)%dis_arr is a character array of size (20x36)
newarray(n,1)=[convertCharsToStrings(dist_arr(n,:))];
newarr=[newarr,s.(newarray(n))];
end
I was wondering if anyone has a possible solution to this issue so I can create an array/structure with all the signals regardless of what the array size is.

回答 (1 件)

GeeTwo
GeeTwo 2022 年 7 月 1 日
What about something like:
function cat=horzcat_pad(a,b);
% How long is the longer matrix?
longer=max(length(a,1),length(b,1));
% Pad any shorter than this with zeros
if length(a,1)<longer, a(longer,1)=0; end
if length(b,1)<longer, b(longer,1)=0; end
% Now horizontally concatenate the possibly padded arrays.
cat=[a b];
end

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by