Comparing Matrices in a Struct
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.

I am attaching a picture
채택된 답변
per isakson
2019년 10월 28일
편집: per isakson
2019년 11월 8일
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
Chris Dan
2019년 11월 2일
Thanks buddy!! :) (Y)
Chris Dan
2019년 11월 6일
Hey, ur code works perfctly for double type matrices,
but when I have sparse matrices, it is not giving me correct results..
Do you know what changes I have to make in it?
per isakson
2019년 11월 7일
"it is not giving me correct results.." next time try to be more specific.
I added a code that handles sparse to the answer.
Chris Dan
2019년 11월 8일
편집: per isakson
2019년 11월 8일
Hey,thanks again for ur help. It is working great!! :) :)
I just have one question, the variables "Out" and S"t are 1x3 struct in the output .but when I see them, they look like 3x1 struct.
So I assume the second value of Out is stored in the second column, instead of second row?
"the variables "Out" and S"t are 1x3 struct in the output" Yes, indeed. (I assume that 'S"t' is a typo.) Running (on R2018b) version 2 of my script returns
>> whos Out S
Name Size Bytes Class Attributes
Out 1x3 2800 struct
S 1x3 2160 struct
"but when I see them, they look like 3x1 struct." I don't understand what you mean.
"So I assume the second value of Out is stored in the second column, instead of second row?" I don't understand.
I added version 3 to the answer. It handles size a bit more explicit and use smaller sizes of the value of model_data.
Running version 3 returns
>> full(Out(1,1).model_data)
ans =
0.19092 0.58951 0.25181 NaN NaN
0.42825 0.22619 0.29044 NaN NaN
0.48202 0.38462 0.61709 NaN NaN
0.12061 0.58299 0.26528 NaN NaN
>> full(Out(1,2).model_data)
ans =
0.82438 0.73025 0.58407 NaN NaN
0.98266 0.34388 0.10777 NaN NaN
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
>> full(Out(1,3).model_data)
ans =
0.90631 0.26073 0.42526 0.17877 0.59852
0.87965 0.59436 0.31272 0.42289 0.47092
0.81776 0.022513 0.16148 0.094229 0.69595
NaN NaN NaN NaN NaN
>>
Chris Dan
2019년 11월 10일
편집: per isakson
2019년 11월 10일
Hey, Thanks again for you help!! :) :)
I just have one more question, it is a different problem but is related matrix addition.
I am copying my code, the thing is I have the logic developed somehow, but it is not giving me correct answers.
B = [ 1 -1 1;
-1 1 1;
1 -1 1];
C = [ 1 -1 2;
-1 1 2;
4 5 5];
D = [ 1 -1 1;
-1 1 4;
5 6 7];
T = zeros(7,7)
d=zeros(3,3,3);
d(:,:,1) = B;
d(:,:,2) = C;
d(:,:,3) = D;
for k = 1:1:3
for i = 1:1:3
for j = 1:1:3
T((i+k)-1,(j+k)-1) = T((i+k)-1,(j+k)-1) + d(i,j,k)
end
end
end
The answer which I am getting is this : which is wrong 

What should I get is
T = [ 1 -1 1 0 0 0 0
-1 1 1 0 0 0 0
1 -1 2 -1 2 0 0
0 0 -1 1 2 0 0
0 0 4 5 6 -1 1
0 0 0 0 -1 1 4
0 0 0 0 5 6 7]
If it is possible, can you help me and tell me what mistake I am making, that would be very nice :)
per isakson
2019년 11월 10일
편집: per isakson
2019년 11월 10일
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
