Combine 2D matrices to form 3D one in Matlab - arrays

I have 3 20x2 double arrays A, B and C. I want to combine them in one 3d array D so that D(:,:,1) will return A, D(:,:,2) will return B and D(:,:,3) will return C.

Using cat to concatenate along the third dimension might be the elegant way -
D = cat(3,A,B,C)
Here, the first input argument 3 specifies the dimension along which the concatenation is to be performed.

Like this?
A = 1*ones(20,2);
B = 2*ones(20,2);
C = 3*ones(20,2);
D = zeros(20,2,3); % Preallocate the D Matrix
D(:,:,1) = A;
D(:,:,2) = B;
D(:,:,3) = C;
D(1,1,1) % prints 1
D(1,1,2) % prints 2
D(1,1,3) % prints 3

Related

String to array in MATLAB

I'm trying to convert an input string "[f1 f2]", where both f1 and f2 are integers, to an array of two integers [f1 f2]. How can I do this?
I have found a way by using sscanf:
f = sscanf(s, "[%d %d]", [1 2]);
where s is the array-like string and f the new array of integers.
You can just use str2num:
f = str2num( "[123 456]" )
% f = [123, 456]
You can use indexing together with strsplit()
my_str = "[32 523]";
split_str = strsplit(my_str, ' '); % split on the whitespace
% The first cell contains "[32" the second "523]"
my_array = [str2num(split_str{1}(2:end)) str2num(split_str{1}(1:end-1))]
my_array =
32 523
When you have more numbers in your string array, you can lift out the first and last elements of split_str out separately (because of the square brackets) and loop/cellfun() over the other entries with str2num.

Matlab. Storing 2D arrays within 3D arrays

I have defined the following 2D function,
Ngrid = 100;
h = 1/(Ngrid-1);
x = 0:h:1;
y = 0:h:1;
[x y] = meshgrid(x,y);
f = exp(-((1-x).^2)./0.45)
and I want to store this function within the 3D array "c",along the "T" dimension,
k = 0.001;
Tend = 1;
T = 0:k:Tend;
c = zeros(length(T),length(x),length(y));
What I have tried is,
c(1:end,:,:) = f;
but it does not work. ¿Any idea of how can I store the same function within this 3D array?
Thanks in advance.
The subscript dimension mismatch is because you are trying to squeeze 100 * 100 elements into a 1001 x 100 x 100 matrix.
You could do this assignment the following way:
c(1,:,:) = f;
c(2,:,:) = f;
...
c(1001,:,:) = f;
But you can accomplish the same thing using repmat
c = repmat(reshape(f, [1, size(f)]), [numel(T), 1 1]);
Or bsxfun
c = bsxfun(#plus, zeros(numel(T), 1), reshape(f, [1, size(f)]));

Skip some columns when searching a matrix in a loop

I have a matrix, c, and I want to search many times for the index of the positive minimum element
d = min(c(c>0));
[x,y] = find(c == d);
but in the next search I want it to skip the old y.
how to do it?
I want to use x and y in some other calculation.
also I want to find this d minimum just within specific columns in the matrix c like:
j from m+1 to n-1
please help
Define mask = zeros(size(c)); before the loop.
And before finding the minimum use,
newc = c + mask;
d = min(newc(newc>0));
[x,y] = find(newc == d);
mask(:,y) = NaN;
I think you can update the c matrix. I mean:
% In the loop, use it:
[x,y]=find(c==d);
c(:, y) = [];
If c matrix is important, you can use a temporary variable equals to c, instead of using c.

MATLAB: Run Data Matrix through a loop and add results to data matrix

What I ultimately need is to input a 2-column matrix, run it through a bunch of conditions and have an output of the 2 original columns plus an additional three.
My initial data matrix I split into several arrays according to time (the second column) and continue with applying my conditions on each array individually:
A = arrayfun(#(x) M(M(:, 2) == x, :), unique(M(:,2)), 'uniformoutput', false);
n = numel(A);
k = 0;
for i = 1:n % for each # of arrays
matrix = A{i}; % array i
dat = size(matrix);
length = dat(1,1); % length of i array
adductName = zeros(length, 1); % preallocate columns
actualMass = zeros(length, 1);
adductMass = zeros(length, 1);
%... continued with conditions here's an example of one
for r = 1:length % for the length of array i
mass = matrix(1,r);
M = mass-1;
k=k+1;
if any(M == matrix(:, 1)) % if any M matches rest of column 1 in array
adductName(k) = 'M';
actualMass(k) = M;
adductMass(k) = mass;
else
adductName(k) = 'None';
actualMass(k) = 0;
adductMass(k) = 0;
adductName, actualMass and adductMass are the three additional columns I need added in my output
My question is, how do I recombine all of my arrays, A{i}'s along with my additional three columns into one data matrix to be outputted?
You can either use the [ ] operator or explicit calls to horzcat or vertcat to concatenate matrices in different ways. See the Creating and Concatenating Matrices documentation part for further reference.

Matlab populate a 3 dimensional array.

I'm quite new to Matlab and am trying to populate a 3 dimensional array. Basically I have 4 lots of 1x81 matrix that I want in one single 4x1x81 matrix. I tried to do this using a for loop to splice each 1x81 into a 4x1x81 but so far haven't had any luck. I'm sure there is a simpler way but need a fresh pair of eyes. Any help would be greatly appreciated, thanks!
I will give you an example and then you apply the same technique. You have to use the colon or : operator to achieve this task.
a=1;b=2;c=3;
Then a 3-D matrix can be formed as:
new3D_Mat(:,:,1)=a;
new3D_Mat(:,:,2)=b;
new3D_Mat(:,:,3)=c;
Output:
>> new3D_Mat
new3D_Mat(:,:,1) =
1
new3D_Mat(:,:,2) =
2
new3D_Mat(:,:,3) =
3
What exactly did you try? How about matrix(1,1,:)=myvector;matrix(2,1,:)=anotherone; or a loop in which you replace the first index with the loop variable? Consider
>> m(1,1,:)=rand(1,3)
m =
(:,:,1) =
0.3478
(:,:,2) =
0.0276
(:,:,3) =
0.5313
Out of curiosity, what is wrong with a 4x81 matrix? (If you already have one, permute may help you get a 4x1x81 3d array.)
Why use a 4x1x81 matrix?
Look at how easy it is to create a 4x81 matrix from four 1x81 matrices.
% Matrix of all ones
a = ones(1, 81);
% Matrix of all twos
b = ones(1, 81);
b = b .*2;
% Matrix of all threes
c = ones(1, 81); c = c .*3;
% Matrix of all fours
d = ones(1, 81); d = d .*4;
% Aggregate
all_of_em = [a; b; c; d];
Run whos to see your variables.
Name Size Bytes Class Attributes
a 1x81 648 double
all_of_em 4x81 2592 double
b 1x81 648 double
c 1x81 648 double
d 1x81 648 double

Resources