Comparing Matrices in a Struct
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chris Dan
am 27 Okt. 2019
Bearbeitet: per isakson
am 10 Nov. 2019
Hello Guys, I am new to MATLAB, I have some matrices in a struct more like an array of arrays.
I want to compare their sizes and see if they are equal or not.
and how to make them equal to the biggest matrice, maybe by adding zeros to the smaller matrices.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/245154/image.jpeg)
I am attaching a picture
0 Kommentare
Akzeptierte Antwort
per isakson
am 28 Okt. 2019
Bearbeitet: per isakson
am 8 Nov. 2019
Try this
S(1).model_data = rand( 4, 10 );
S(2).model_data = rand( 2, 10 );
S(3).model_data = rand( 3, 11 );
Out = cssm_( S );
function Out = cssm_( S )
sz(1) = max( arrayfun( @(s) size( s.model_data, 1 ), S ) );
sz(2) = max( arrayfun( @(s) size( s.model_data, 2 ), S ) );
Out = struct( 'model_data', repmat( {nan(sz)}, 1,numel(S) ) );
for jj = 1 : numel(S)
sz = size(S(jj).model_data);
Out(jj).model_data(1:sz(1),1:sz(2)) = S(jj).model_data;
end
end
In response to comment
This will handle sparse, test it. ( cssm_ assumes that all values of data_model are either full or sparse.)
S(1).model_data = sparse( rand( 4, 10 ) );
S(2).model_data = sparse( rand( 2, 10 ) );
S(3).model_data = sparse( rand( 3, 11 ) );
Out = cssm_( S );
function Out = cssm_( S )
sz(1) = max( arrayfun( @(s) size( s.model_data, 1 ), S ) );
sz(2) = max( arrayfun( @(s) size( s.model_data, 2 ), S ) );
if issparse( S(1).model_data )
Out = struct( 'model_data', repmat( {sparse(nan(sz))}, 1,numel(S) ) );
else
Out = struct( 'model_data', repmat( {nan(sz)}, 1,numel(S) ) );
end
for jj = 1 : numel(S)
sz = size(S(jj).model_data);
Out(jj).model_data(1:sz(1),1:sz(2)) = S(jj).model_data;
end
end
Version 3 in response to a later comment
S(1,1).model_data = sparse( rand( 4, 3 ) );
S(1,2).model_data = sparse( rand( 2, 3 ) );
S(1,3).model_data = sparse( rand( 3, 5 ) );
Out = cssm_( S );
function Out = cssm_( S )
sz(1) = max( arrayfun( @(s) size( s.model_data, 1 ), S ) );
sz(2) = max( arrayfun( @(s) size( s.model_data, 2 ), S ) );
if issparse( S(1).model_data )
Out = struct( 'model_data', repmat( {sparse(nan(sz))}, size(S) ) );
else
Out = struct( 'model_data', repmat( {nan(sz)}, size(S) ) );
end
for jj = 1 : numel(S)
sz = size(S(jj).model_data);
Out(jj).model_data(1:sz(1),1:sz(2)) = S(jj).model_data;
end
end
8 Kommentare
per isakson
am 10 Nov. 2019
Bearbeitet: per isakson
am 10 Nov. 2019
It's far from obvious to me what algorithm you try to implement and I cannot easily deduce it from the example.
Regarding your code I directly observe that
- all T_actual(6:7,:) and all T_actual(:,6:7) are equal to zero
- the maximum values of the counters, k, i, j, are 3
- the maximum value of (i+k)-1 and (j+k)-1, respectively is 5, which explains why all T_actual(6:7,:) and all T_actual(:,6:7) are equal to zero
- all T_actual(5,1:5)==T_expected(7,3:7)
Proposal: Post a new question in which you describe in some detail the algorithm you try to implement.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures 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!