- use a container array (e.g. a cell array or structure array) to hold the differently-sized data,
- or you would have to ensure the data are the same sizes (e.g. by modifying the data before concatenating or by padding&rearranging the data after concatenating together). For example:
Extracting arrays from a structure - different number of array elements
1 回表示 (過去 30 日間)
古いコメントを表示
I am having problems extracting arrays from a structure that I created. One of the fields returns a different number of values.
Hypo2 =
1×161 struct array with fields:
datim
otime
file
volctype
lat
lon
dep
mag
>> size( Hypo2 )
ans =
1 161
>> size( [Hypo2.datim] )
ans =
1 161
>> size( [Hypo2.mag] )
ans =
1 160
Hypo2.mag contains numeric values and I'm guessing that there is a NaN in one of them.
How can I extract the arrays so that they are all of the same size? I thought that [] was the way to do it.
I attach a mat file with Hypo2 - there are more fields than I listed above.
0 件のコメント
採用された回答
Stephen23
2023 年 4 月 5 日
編集済み: Stephen23
2023 年 4 月 5 日
"I'm guessing that there is a NaN in one of them."
A NaN is scalar, so would not cause this. However an empty array could cause this:
S = load('Hypo2.mat');
H = S.Hypo2;
unique(cellfun(@numel,{H.datim})) % all scalar
unique(cellfun(@numel,{H.mag})) % some are empty
"How can I extract the arrays so that they are all of the same size?"
You would have to either
X = cellfun(@isempty,{H.mag});
[H(X).mag] = deal(NaN);
V = [H.mag]
"I thought that [] was the way to do it."
Square brackets are a concatenation operator. If the sizes of the things being concatenated together are different, why should the concatenation operator magically make them the same size? How would it even know what size you expect the output to be (except by using the sizes of the input data... which is exactly what it does now). Why should these be the same size?:
[0,1,2]
[0,[],2]
See also:
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!