- 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 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
dormant
am 5 Apr. 2023
Kommentiert: dormant
am 6 Apr. 2023
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 Kommentare
Akzeptierte Antwort
Stephen23
am 5 Apr. 2023
Bearbeitet: Stephen23
am 5 Apr. 2023
"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:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!