MATLAB - extracting rows of a matrix - matlab

a = [1 2; 3 4; 5 6] I want to extract the first and third row of a, so I have x = [1; 3] (indices of rows).
a(x) doesn't work.

Like this: a([1,3],:)
The comma separates the dimensions, : means "entire range", and square brackets make a list.

In MATLAB if one parameter is given when indexing, it is so-called linear indexing. For example if you have 4x3 matrix, the linear indices of the elements look like this, they are growing by the columns:
1 5 9
2 6 10
3 7 11
4 8 12
Because you passed the [1 3] vector as a parameter, the 1st and 3rd elements were selected only.
When selecting whole columns or rows, the following format shall be used:
A(:, [list of columns]) % for whole columns
A([list of rows], :) % for whole rows
General form of 2d matrix indexing:
A([list of rows], [list of columns])
The result is the elements in the intersection of the indexed rows and columns. Results will be the elements marked by X:
A([2 4], [3 4 5 7])
. . C C C . C
R R X X X R X
. . C C C . C
R R X X X R X
Reference and some similar examples: tutorial on MATLAB matrix indexing.

x = a([1 3]) behaves like this:
temp = a(:) % convert matrix 'a' into a column wise vector
x = temp([1 3]) % get the 1st and 3rd elements of 'a'

you could write a loop to iterate across the rows of the matrix:
for i = [1,3]
a(i,:)
end

type this: a([1 3],[1 2])
the output is
ans =
1 2
5 6

Related

Convert a cell containing matrices to a 2d matrix

I want to find easy way to convert a 1x324 cell array which contains matrices to a 2-dimensional matrix.
Each of the cell array's elements is a matrix of size 27x94, so they contain 2538 different values. I want to convert this cell array of matrices to a 324x2538 matrix - where the rows of the output contain each matrix (as a row vector) from the cell array.
To clarify what my data looks like and what I'm trying to create, see this example:
matrix1 = [1,2,3,4,...,94 ; 95,96,97,... ; 2445,2446,2447,...,2538]; % (27x94 matrix)
% ... other matrices are similar
A = {matrix1, matrix2, matrix3, ..., matrix324}; % Matrices are in 1st row of cell array
What I am trying to get:
% 324x2538 output matrix
B = [1 , 2 , ..., 2538 ; % matrix1
2539 , 2540, ..., 5076 ; % matrix2
...
819775, 819776, ..., 822312];
The cell2mat function does exactly that. The doc example:
C = {[1], [2 3 4];
[5; 9], [6 7 8; 10 11 12]};
A = cell2mat(C)
A =
1 2 3 4
5 6 7 8
9 10 11 12
You have your matrix now, so just rework it to contain rows:
B = rand(27,302456); % your B
D = reshape(B,27,94,324); % stack your matrices to 3D
E = reshape(D,1, 2538,324); % reshape each slice to a row vector
E = permute(E,[3 2 1]); % permute the dimensions to the correct order
% Based on sizes instead of fixed numbers
% D = reshape(B, [size(A{1}) numel(A)]);
% E = reshape(D,[1 prod(size(A{1})) numel(A)]);
% E = permute(E,[3 2 1]); % permute the dimensions to the correct order
Or, to one line it from your B:
B = reshape(B,prod(size(A{1})),numel(A)).'
One way to write this would be using cellfun to operate on each element of the cell, then concatenating the result.
% Using your input cell array A, turn all matrices into column vectors
% You need shiftdim so that the result is e.g. [1 2 3 4] not [1 3 2 4] for [1 2; 3 4]
B = cellfun(#(r) reshape(shiftdim(r,1),[],1), A, 'uniformoutput', false);
% Stack all columns vectors together then transpose
B = [B{:}].';
Now I found the solution and I will add it here if anyone have similar problems in future:
for ii = 1:length(A)
B{ii} = A{ii}(:);
end
B = cell2mat(B).';

Multiply each column in a matrix by corresponding row in another and sum results in Matlab

Lets say I have matrices A = [1 2; 3 4], B = [4 3; 2 1]. I want to multiply each column from matrix A ([1; 3], [2; 4]) by the corresponding row in matrix B ([4 3], [2 1]) and sum resulting matrices. I have came up with the following code:
C = zeros(size(A));
for i = 1 : size(A, 1)
C = C + A(:, i) * B(i, :);
end
Could it be rewritten using some math trick or matlab function to get rid of the for loop?
I see there is unclarity in my question regarding the result I want. The result should exactly mimic provided Matlab code, therefore I seek one matrix which is given by the matrix summation of the intermediate matrices that are created by multiplying each column vector with corresponding row vector from both matrices. For this specific example, it would be given by
C = A(:, 1) * B(1, :) + A(:, 2) * B(2, :);
I am just looking for some generic, for-loop less version for any matrices of compatible dimensions.
I just tried out my suggestion in the comments, and it seems to work with this octave tester:
Short form (only works in Octave):
A = [1 2; 3 4], B = [4 3; 2 1]
X = sum((A * B)(:))
Long form (Matlab):
A = [1 2; 3 4]
B = [4 3; 2 1]
C = A * B % Stop here if you want the exact result from your Matlab code
x = sum(C(:)) % To get the sum of the resulting matrix
Sources:
https://www.tutorialspoint.com/matlab/matlab_matrix_multiplication.htm
https://www.mathworks.com/matlabcentral/newsreader/view_thread/51252
Update, based on your update:
Output of A * B:
8 5
20 13
Output of your code:
8 5
20 13
It appears that
C = zeros(size(A));
for i = 1 : size(A, 1)
C = C + A(:, i) * B(i, :);
end
is equivalent to the matrix multiplication
C = A*B
for square matrices A and B.
You can also do this in MATLAB, to get the sum.
C=ones(1,2)*A*B*ones(2,1)
The general form would be
C=ones(1,size(A,1))*(A*B)*ones(size(B,2),1);
EDIT
I see you updated your question for clarity. The matrix product can be calculated directly
C = A*B;
as pointed out by jodag.
This works provided you follow rules of linear algebra where inner dimensions of your matrices are the same (i.e. when number of columns in A match the number of rows in B; size(A,2)==size(B,1)).

How do I raise a vector to a vector in MATLAB? [duplicate]

I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = xn for each x in X and n in N.
For example, the following input:
X = [2 3 4]'
N = [1 2 3]
should produce:
ans = [2 4 8; 3 9 27; 4 16 64]
Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)?
Use bsxfun:
bsxfun(#power, X, N)
This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the following syntax which is more robust:
bsxfun(#power, X(:), N(:).')
This is probably a bit sloppier than the bsxfun answer, but you could use meshgrid:
E = X.^(meshgrid(N)')
This assumes both X and N are row vectors. If both are column vectors then it becomes:
E = X.^(meshgrid(N))

MATLAB/OCTAVE Extracting elements from different sized vectors in a cell array

I have a simple question but I can't figure it out or find it anywhere.
I have a cell array where c{1} is a vector and c{2} is a vector but of different lengths, up to c{i}. What I want is one vector that is [c{1};c{2};c{3}...c{i}]. What is the most efficient way to do this?
The following one-liner even works for completely inconsistent inputs:
result = [cell2mat(cellfun(#(x) x(:), A, 'uni', 0)')]'
Example:
for:
A{1} = [1, 2, 3, 4, 5];
A{2} = [6; 7; 8; 9];
A{3} = [10, 12; 11, 13];
it returns:
result =
1 2 3 4 5 6 7 8 9 10 11 12 13
Matlab/Octave allows this king of really-not-efficient but very-convenient notation, assuming a is a structure only containing column-vectors:
x = []; #% A fresh new vector/matrix/tensor, who knows?
for i=1:numel(a) #% parse container item by item
x = [x;a{i}]; #% append container item a{i} to x in a column-fashion way
end
This will works but it is bloody inefficient since it will reallocate x each for step and it is not bulletproof (no error handling, no type checking): therefore it will fail if it encounters anything (matrix, string, row vector) but column vector which are likely to be found in such containers.
Anyway, it will ease a not-so-stringent-and-heuristic design, but please consider reimplementing when robust design is needed.
You can padding each cell with zeros, and align the lengths to the longest cell vector. It is done in a loop by iterating each cell vector.
This depends on whether the vectors in c are row or column vectors. But usually the fastest and most compact ways are:
c={[1 2 3], [4 5 6 7 8], [9 10]}
cell2mat(c)
cat(2, c{:})
or
c={[1 2 3]', [4 5 6 7 8]', [9 10]'}
% cell2mat(c) % Doesn't work.
cat(1, c{:})
so personally, I prefer cat.
In Matlab; without loops:
If the cell array contains column vectors and you want to arrange them into one big column vector:
result = vertcat(c{:}); %// vertically concat all vectors
Example:
>> c = {[1;2], [1;2;3]};
>> result = vertcat(c{:})
result =
1
2
1
2
3
If the cell array contains row vectors, you can arrange them as rows of a matrix, filling non-existent values with NaN (or any other value):
M = max(cellfun(#numel, c)); %// max length of vectors
c2 = cellfun(#(row)[row NaN(1,M-numel(row))], c, 'uni', 0); %// fill with NaN
result = vertcat(c2{:}); %// concat all equal-size row vectors into a matrix
Example:
>> c = {[1 2], [1 2 3]};
>> M = max(cellfun(#numel, c));
>> c2 = cellfun(#(row)[row NaN(1,M-numel(row))], c, 'uni', 0);
>> result = vertcat(c2{:})
result =
1 2 NaN
1 2 3

Vectorized exponentiation

I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = xn for each x in X and n in N.
For example, the following input:
X = [2 3 4]'
N = [1 2 3]
should produce:
ans = [2 4 8; 3 9 27; 4 16 64]
Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)?
Use bsxfun:
bsxfun(#power, X, N)
This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the following syntax which is more robust:
bsxfun(#power, X(:), N(:).')
This is probably a bit sloppier than the bsxfun answer, but you could use meshgrid:
E = X.^(meshgrid(N)')
This assumes both X and N are row vectors. If both are column vectors then it becomes:
E = X.^(meshgrid(N))