Merge multiple 3D arrays matlab - arrays

I have a 3 sets of CT data each in it's own 700x700x512 array and I want to merge them into just one single array.
I had a look at the cat() function but didn't really understand how you set the dim variable- i.e. say for two simple 3x3x3 arrays, A & B, can I use AB_merge = cat(dim, A, B);
Thanks for any help!

The dim variable sets along which dimension you want to concatenate the images.
So if you want them 'on top of each other' that is along the 3rd dimensions:
AB_merge=cat(3, A, B);
If it is side by side along the x-axis:
AB_merge=cat(1, A, B);
etc.

Related

MATLAB: combine two cell arrays of string at a specific position

I have two cells of strings: A(a,1) and B(b,1), where a>b.
I want to combine A with B, adding B at a certain position of A.
Notice that in A there are no blank rows, so I suppose that first I have to add b blank rows in A and then concatenate B at a specific position.
Any suggestion?
Thank you :)
Suppose that you want to place B in the position p of A. You can use indexing and concatenation of cell arrays like any other array types:
A = [A(1:p - 1, 1); B; A(p:end, 1)];

Matlab accessing the inner array of a 2d array

Matlab is really driving me crazy on this point. I just want to access the inner array of a 2d array.
E.g.:
A = [1,1; 2,2; 3,3]
B = [4,4; 5,5; 6,6]
C = [7,7; 8,8; 9,9]
D = [0,0; 1,2; 3,4]
E = [A,B,C,D]
how do I get e.g. B out of E again?
By this I mean in the exact same writing styl like X = [4,4; 5,5; 6,6]
The syntax you use concatenates the array to a new one, it is not an array of arrays.
If you want an array of arrays, you could use a cell, E = {A,B,C,D}. Then you can get B back using E{2}.
Cells are general containers, each element can contain whatever you want, it does not have to be the same data type. See What is a cell?
You concatenated A, B, C, D horizontally into a new array E. That is not array of arrays, as the other answer pointed out. Suppose the new array is what you want. If you like to extract original B from E, you will need to know A and B's size, in this case both are 3x2. So You can do following:
X = E(:, 3:4); % 3 is size(A,2)+1, numel(3:4) is size(B,2)
Also I think you did not really mean "writing style", since that is just a way to write assignment.

Multiplying array columns by vector

I'm new to R and I am certain that this is simple yet I can't seem to find an answer. I have an array [36,21,12012], and I need to multiply all of the columns by a vector of the same length to create a new array of the same dimensions.
If v is your vector and a is your array, in your case it would be as simple as v * a, because arrays are built column-wise. But in general, you would use sweep. For example to multiply along the rows, sweep(a, MARGIN=2, STATS=v, FUN='*').

Reassemble matrix

I want to extract the matrices from the matrix B on the nodes I defined by perm(:,i) which are the ith column matrix of emp and I want to calculate A1= B(perm(:,1),perm(:,1)), A2=B(perm(:,2),perm(:,2)) that is make a loop such that:
for i=1:n
I got A1, A2 and so on –
From what i can discern, it seems that you want to dynamically extract a sub-matrix from matrix B, according to the perms matrix. Your problem, it seems, is that you cannot do this in a loop, because you don't know how to dynamically create matrices. Your solution is to create a cell and input each sub-matrix in the cell. Thus, if A is your cell then,
A=cell(1,N);
for i=1:N
A{i}=B(perm(:,i),perm(:,i));
end
You can get the matrix Ai from the cell using,
Ai=A{i}; %notice the curly braces {}
If you really want variables named A1, A2, &c, you can use eval:
for i = 1:N
eval(['A' num2str(i) ' = B(perm(:,i),perm(:,i))']);
end
However, it's probably not a good idea and you'd better use cells as shown by #Jorge. Cells can contain anything and are much more convenient.

How can I combine these two arrays into a matrix?

In MATLAB, if I define 2 matrices like:
A = [1:10];
B = [1:11];
How do I make matrix C with column 1 equal to A and column 2 equal to B? I cannot find any answers online. Sorry if I used the wrong MATLAB terminology for this scenario.
Well, to accomplish this you first need to make sure that A and B are the same length. In your example, A has 10 elements and B has 11, so that won't work.
However, assuming A and B have the same number of elements, this will do the trick:
C = [A(:) B(:)];
This first reshapes A and B into column vectors using single-colon indexing, then concatenates them horizontally.
if A,B same length, then can just type
C=[A' B']

Resources