How to build on an existing struct 4x4xn matrix?
조회 수: 4 (최근 30일)
이전 댓글 표시
I have an struct, for example...
A = struct();
where we have a 4x4 matrix
A.G = rand(4,4);
I want to build on values within the (4x4) matrix but stack new values in a n'th dimension.
How do I build on the maxtrix in the n'th dimesnion if I don't know how many non-zero values exist in respective field A.G(1,1,n), A.G(1,2,n), A.G(2,2.n) and A.G(4,4,n). the n't value can stack randomly. I wan't to know how many non-zero values lies behind for example A.G.(1,2,n) where n is a unkown value depending how many values i already put into A.G(1,2,n), which i don't keep track on.
댓글 수: 4
Mrutyunjaya Hiremath
2020년 4월 28일
Hello Happy PhD,
do you want to add next value where there are 0's are there?
채택된 답변
Mrutyunjaya Hiremath
2020년 4월 28일
Hello Happy PhD,
Try this one...
clc;
clear all;
A.G(:,:,1) = [1 2 1 1 ; 4 9 5 6 ];
A.G(:,:,2) = [0 1 0 0; 0 6 0 0];
A.G(:,:,3) = [0 5 0 0; 0 0 0 0];
N = 10; % random number
idx = find(A.G == 0);
if ~isempty(idx)
A.G(idx(1)) = N;
else
n = size(A.G,3);
A.G(1,1,n+1) = N;
end
disp(A.G);
댓글 수: 0
추가 답변 (1개)
Rik
2020년 4월 28일
It sounds like you can just assign that value. Matlab will expand the array and fill empty positions with 0.
A.G=cat(3,[ 1 2 0 1 ; 4 9 5 6 ], [ 0 1 0 0; 0 6 0 0],[0 5 0 0; 0 0 0 0 ]);
A.G(1,2,4)=8;%Matlab fills the 4th page with zeros
disp(A.G)
댓글 수: 2
Rik
2020년 4월 28일
You can find the first empty page like this:
[r,c,val]=deal(1,2,8);
A.G=cat(3,[ 1 2 0 1 ; 4 9 5 6 ], [ 0 1 0 0; 0 6 0 0],[0 5 0 0; 0 0 0 0 ]);
N_by_rc=sum(logical(A.G),3)+1;
n=N_by_rc(r,c);
A.G(r,c,n)=val;
clc,disp(A.G)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!