Create a matrix from three column vectors - arrays

I have three column vectors, then I want to creat matrix from this column victors
A1(:);
A2(:);
A3(:)
each column vectors has 25 element then the new matrix C will be a matrix with 3x25
I want to make A1(:) the first column of matrix c
A2(:) second column
A3(:) third column

Use cat to concatenate along dimension 1 or 2 depending on how you input those three vectors.
Thus, you can use -
C = cat(2,A1(:),A2(:),A3(:)).'
Or
C = cat(1,A1(:).',A2(:).',A3(:).')
Of course, you can skip (:)'s, if you know that all those are column vectors.
The above two approaches assumes that you intend to get an output of size 3 x N, where is N is the number of elements in the column vectors. If you were looking to get an output of size N x 3 , i.e. where each column is formed from the elements of column vectors A1, A2 and so on, just drop the transpose from the first of the two approaches mentioned above. Thus, use this -
C = cat(2,A1(:),A2(:),A3(:))

Related

MATLAB- Cell arrray numeric data splitting and extracting

My requirement is to create generate numerous complex matrices of 15x15 and extract the diagonal elements and upper triangle elements as column vectors and finally combine them into a single column vector.
As a part of the implementation I have done some code which looks similar to this
I have created a program to generate 6 complex matrices using for loop and it is stored in a cell.
Code 1:-
clear all;
close all;
clc;
N=15;
k=6;
C=cell(k,1)
for j = 1:k
C{j}= rand(N,N) + 1i*rand(N,N); % Complex Matrix
end
Now the cell C 6 x1 contains 15x15 complex double numeric data in each cell array.
For extracting the diagonal elements extract the diagonal elements and upper triangle elements as column vectors and finally combine them into a single column vector for a Matrix C I have the following code.
Code 2:-
D = diag(C)
L = triu(C,1)
U = tril(C,-1)
V= [D; real(U(:)); real(L(:))]
I would like to combine this Code2 approach for the Cell C obtained in Code1 by splitting the each array in cell C- 6x1 with 15x15 in each cell into different arrays/ matrices of 15x15 order and then do the code2 operation.
Is it better to use Cell approach to create a loop of matrices or is there any other way to do. Please guide

Calculating standard deviation on data stored in a cell array of cell arrays

I have a cell array A Mx3 in size where each entry contains a further cell-array Nx1 in size, for example when M=9 and N=5:
All data contained within in any given cell array is in vector format and of equal length. For example, A{1,1} contains 5 vectors 1x93 in size whilst A{1,2} contains 5 vectors 1x100 in size:
I wish to carry out this procedure on each of the 27 cells:
B = transpose(cell2mat(A{1,1}));
B = sort(B);
C = std(B,0,2); %Calculate standard deviation
Ultimately, the desired outcome would be, for the above example, 27 columns (9x3) containing the standard deviation results (padded with 0 or NaNs to handle differing lengths) printed in the order A{1,1}, A{1,2}, A{1,3}, A{2,1}, A{2,2}, A{2,3} and so forth.
I can do this by wrapping the above code into a loop to iterate over each one of the 27 cells in the correct order however, I was wondering if there was a clever cellfun or more succinct method to accomplish this particularly without the use of a loop?
You should probably realize that cellfun is essentially a glorified for loop over cells. There's simply extra error checking and all that to ensure that the whole thing works. In any case, yes it's possible to do what you're asking in a single cellfun call. Note that I am simply going to apply the same logic as you would have in a for loop with cellfun. Also note that because you're using cell arrays, you have no choice but to iterate over the entire master cell array. However, what you'll want to do is pad each resulting column vector in each output in the final cell array so that they all share the same length. We can do that with another two cellfun calls - one to determine the largest vector length and another to perform the padding operation.
Something like this could work:
% Step #1 - Sort the vectors in each cell array, then find row-wise std
B = cellfun(#(x) std(sort(cell2mat(x).'), 0, 2), A, 'un', 0);
% Step #2 - Determine the largest length vector and pad
sizes = cellfun(#numel, B);
B = cellfun(#(x) [x; nan(max(sizes(:)) - numel(x), 1)], B, 'un', 0);
The first line of code takes each element in A, converts each cell element into a N x 5 column matrix (i.e. cell2mat(x).'), we then sort each column individually with sort, then take the standard deviation row-wise. Because the output is ultimately a vector, we must make sure that the 'UniformOutput' flag is 0, or 'un=0'. Once we complete the standard deviation calculation, we determine the total number of elements for each resulting column vector for all cell elements, determine the largest size then use another cellfun call to pad these vectors so they all match the same size.
To finally get your desired output, you need to transpose the cell array, then unroll the elements in column major order. Remember that MATLAB accesses things in column major, so a common trick to get things in row-major (what you want) as opposed to column major is to first transpose, then unroll in column-major fashion to perform a row-major readout. Doing this in one line is tricky, so you'll need to not only transpose the cell array, you must use reshape to ensure that the elements are read out in row major format, but then ensuring that the result is placed in a row of cells, then call cell2mat so you can piece these vectors together. The final result should be a 27 column matrix where we have pieced all of these vectors together in a single row-wise fashion:
C = cell2mat(reshape(B.', 1, []));

Adding multiple rows in Array

I have an array A size of 16X16 and I want to add first 3 rows out of 16 in A. What is the most efficient solution in MATLAB?
I tried this code but this is not efficient because I want to extend it for large arrays:
filename = 'n1.txt';
B = importdata(filename);
i = 1;
D = B(i,:)+ B(i+1,:)+ B(i+2,:);
For example, if I want to extend this for an array of size 256x256 and I want to extract 100 rows and add them, how I will do this?
A(1:3,:);%// first three rows.
This uses the standard indices of matrix notation. Check Luis's answer I linked for the full explanation on indices in all forms. For summing things:
B = A(1:100,:);%// first 100 rows
C = sum(B,1);%// sum per column
D = sum(B,2);%// sum per row
E = sum(B(:));%// sum all elements, rows and columns, to a single scalar

finding index of rows in matlab

i have a matrix B of N*3 dim. I want to find the indices of B whose column 3 has value 1.
I used the command [~,id]=ismember(1,B(:,3)). id returns only value 1 even though there are many rows in the matrix which has the column 3 with value 1. Can any one point out what is wrong in the command?
Rather do:
id = find(B(:,3)==1)
but as an aside, to use ismember you should swap your input [~,id]=ismember(B(:,3),1).

Inserting a constant array in between every other column

Let's say, I have an M x N matrix. Now, I want to insert a constant M x 1 column vector (say all 1's) in between each of the N columns. Therefore, my resulting matrix would be of dimension (M x (2*N-1)), with every other column being 1's.
Is there an easy way to do that?
Vertically concatenate a matrix of ones, reshape, and cut off the last column of ones. For a matrix A:
B = reshape([A; ones(size(A))],size(A,1),[]);
B(:,end)=[]
Here is another way to do it, using the possibility of out of bounds indexing in assignments:
M(:,1:2:end*2)=M;
M(:,2:2:end)=1
If you don't mind creating a temporary matrix, one way to do it would be to do the following:
old_matrix = rand(M,N); % Just for example
new_matrix = ones(M,2*N-1);
new_matrix(:,1:2:end) = old_matrix;
Note that for an arbitrary constant matrix, you could replace the second line with the following:
new_matrix = repmat(const_array,1,2*N-1);

Resources