MATLAB Answers

How to build on an existing struct 4x4xn matrix?

2 views (last 30 days)
Happy PhD
Happy PhD on 28 Apr 2020
Commented: Rik on 28 Apr 2020
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 Comments

Show 1 older comment
Happy PhD
Happy PhD on 28 Apr 2020
I have an 2x4x3 matrix for example
A.G(:,:,1) = [ 1 2 0 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 ];
I want to add an value to for example A.G(2,2,n) or A.G(1,1,n) or A.G(1,2,n) but I don't know how many values we have in respective X and Y position in n'th dimension, where coordinate system is the 1'st dimension is A.G(X,Y,1) = [ (x1,y1) (x1,y2) (x1,y3) (x1,y4); (x2, y1) (x2,y2) (x2,y3) (x2,y4)]; if A.G(:,:,1) is the 1st dimension, A.G(:,:,2) is the 2nd dimesnion and A.G(:,:,3) is the 3rd dimesnion etc... So that means ...A.G(1,2,1) = 2 and A.G(2,2,1) =9 and A.G(2,2,2) = 6.. etc...
Hopefully I am making sense now. Note 0 is a non-value here.
I want to for example add a value to A.G(1,2,4) = 8 or A.G(1,1,2) = 8 .. but I don't know how many values are in A.G(1,2,n) or A.G(1,1,n) where the n-value is unkown. How do I add next value there. Note 0 is a non-value here.
Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 28 Apr 2020
Hello Happy PhD,
do you want to add next value where there are 0's are there?
Happy PhD
Happy PhD on 28 Apr 2020
That is correct! I want to add values after all non-zero values. And increase the matrix size in the third dimension when needed.

Sign in to comment.

Accepted Answer

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 28 Apr 2020
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 Comments

Sign in to comment.

More Answers (1)

Rik
Rik on 28 Apr 2020
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 Comments

Happy PhD
Happy PhD on 28 Apr 2020
In my application I don’t know how my matrix looks like, so I don’t know I can add a value in A.G(1,2,4) -where 4 is unknown to me. But I want add a number in A.G(1,2,n) where n is the next available position.
It might be zeroes here .. I want to add the next value after the next non-zero value in that dimensional ‘column‘.
Rik
Rik on 28 Apr 2020
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)

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!