Commas in arguments of an array - matlab

The second argument in plot puzzles me, removed the "()" after S and no difference. What is the point of it?

In this case where S is a matrix, the arguments that follow are used to take a selection of the matrix in each dimension. The : operator is used to select entries in a range (e.g. 2:end) and when used alone (i.e. :) selects all entries in a range.
The following example selects all rows (:) of the second column (2). Note that S and S(:, :, 1) in this case are identical as this array does not contain a third dimension. I believe this is also the case for your example.
>> S = [1, 2, 3; 4, 5, 6; 7, 8, 9]
S =
1 2 3
4 5 6
7 8 9
>> S(:, 2)
ans =
2
5
8

Related

Debugging the error "Dimensions of arrays being concatenated are not consistent" in MATLAB

I have a function VanderPol() which is supposed to give a vector output, but it doesn't seem to work. It is just three lines of code but I cannot seem to find the bug.
The function is
function [output] = VanderPol(y, i)
output = [y(2,i); (1-y(1,i)^2)*y(2,i) -y(1,i)];
end
and it is called as
z = [1 2 3;
4 5 6];
VanderPol(z,1)
I recieve an error message stating that VanderPol(z,1) is faulty, but no hint why. The exact error message is shown below. Can anyone spot the bug?
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
This is a bit of an edge case: You can construct arrays in MATLAB by separating elements either by a comma , or a space . Thus, the following ways both work and give the same result:
a = [1, 2, 3]
b = [1 2 3]
When building matrices, this works similarly, and rows are separated by a semicolon or a new line, i.e. we have the following equivalent possibilities:
A = [1, 2, 3; 4, 5, 6]
B = [1 2 3; 4 5 6]
C = [1, 2, 3
4, 5, 6]
D = [1 2 3
4 5 6]
Now to your example: your array is the following:
[y(2,i); (1-y(1,i)^2)*y(2,i) -y(1,i)]
The first row contains one element y(2,i). The second row, however, is interpreted as two elements: (1-y(1,i)^2)*y(2,i) and -y(1,i), due to the space between these parts. I.e. MATLAB thinks you are using a space to separate two parts of an array like in b above. It interprets the input like the following:
[y(2,i); (1-y(1,i)^2)*y(2,i), -y(1,i)]
If you paste the code into MATLAB, you will thus get an error complaining that it is not possible to have an array with 1 element in the first and 2 elements in the second row:
>> [y(2,i); (1-y(1,i)^2)*y(2,i) -y(1,i)]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
To solve the problem you have to tell MATLAB that there is only one element in the second row, given by the subtraction (1-y(1,i)^2)*y(2,i) -y(1,i). Here are some ways to do that:
output = [y(2,i); (1-y(1,i)^2)*y(2,i) - y(1,i)]; % spaces on both sides of -
output = [y(2,i); (1-y(1,i)^2)*y(2,i)-y(1,i)]; % no spaces around -
output = [y(2,i); ((1-y(1,i)^2)*y(2,i) -y(1,i))]; % parentheses around everything

Removing elements from arbitary columns in a Matrix in MATLAB

Suppose I have a matrix in MATLAB.
>> m = [1 2 3; 4 5 6; 7 8 9]
m =
1 2 3
4 5 6
7 8 9
I have a list of indices, and I would like elements at those indices to be removed from the matrix.
The indices may belong to any arbitrary row or column. However, I can guarantee that if I were to remove an element from a row, I must remove an element from all other rows.
Once all the elements are removed, any "gaps" in the matrix should be addressed by shifting elements to the left.
% for example, removing m(1, 1), m(2, 2), m(3, 3) should yield
m =
2 3
4 6
7 8
% it will NOT yield the following because the elements were shifted up, not to the left.
M =
4 2 3
7 8 6
% removing only m(1, 1) would also be invalid,
% because I must remove an element from all other rows.
What would be the most efficient way to perform this operation for arbitrary number of indices?
As you need the elements shifted up, the solution is a two-step one. First transpose the matrix, remove the corresponding elements, and then reshape and transpose the result. (If shifting up were allowed, then you wouldn't need to transpose). Assuming the indices are stored in a matrix, remove, then:
m=[1,2,3;4,5,6;7,8,9];
remove=[1,1;2,2;3,3];
copy=m.';
width=size(copy,2);
copy(sub2ind(size(copy),remove(:,2),remove(:,1)))=[];
m=reshape(copy,[],width).'
I think that solves the problem...

find the rank of an element in a vector in matlab

I have the task to combine two vectors in the following way. The input are two vectors with the first one indicating the indices of the dividors of the groups and the second one indicating the elements we are trying to classify. For example, vector [1,3,5,9] means the first group consists of 1, the second group consists of 2 and 3, the third group consists of 4 and 5, and the forth one consists of 6, 7, 8 and 9, etc. In this case, if the second vector is [2,4,6], then the output we get is [2,3,4].
I know how to impliment this in matlab with for loops. My question is: is there anyway to do it without for loops? Many thanks for your time and attentions.
EDIT:
scaleVtr=[1,3,5,9];
>> eltVtr=[2,4,6];
>> j=1; output=[];
>> for i=1:size(eltVtr,2)
while(true)
if eltVtr(i)<=scaleVtr(j)
output= [output,j];
break;
else j=j+1;
end
end
end
>> output
output =
2 3 4
qq = [1 3 5 9];
qq2 = [2 4 6];
ceil(interp1(qq,1:numel(qq),qq2))
This can also be done with bsxfun:
v1 = [1,3,5,9];
v2 = [2,4,6];
result = sum(bsxfun(#gt, v2(:).', v1(:)), 1) + 1;

nth permutation of a vector in MATLAB

Suppose I have a vector of integers like this:
A = [1 2 3]
What I need is nth permutation of vector A. As we now a vector of n numbers has n! permutation, For example some permutation of A is:
[1 2 3]
[1 3 2]
[2 1 3]
[2 3 1]
...
Is there any built-in function for calculating nth permutation? if not, can anyone please offer me a efficient algorithm for calculate it? Any suggestion would be highly appreciated
I found my answer from #Divakar comment (special thanks to #Divakar)
What I need is:
% this my vector 1, 2, 3 , ..., N
A = 1 : N;
P = perms(A);
% nth permutation of A is nth row of P
nthPerm = P(n, :);
If A is just the trivial sequence 1:N, as #Divakar said, the command
perms(1:N)
produces the permutations you need.
If A is an array whose content is generic and whose length is N, perms can be used to obtain the indices allowing the permutations, i.e.
A_permutations = A(perms(1:N))
Example:
given
A =
3 7 9
A(perms(1:3))
9 7 3
9 3 7
7 9 3
7 3 9
3 7 9
3 9 7
perms(v) works for the n! case,
http://www.mathworks.de/matlabcentral/fileexchange/11462-npermutek/content/npermutek.m
works for the n^n or n^k case.
If you like doing stuff in less lines of code, you can also do:
A=1:N;
nthPerm=getfield(perms(A),{n,A})
Note that this is only valid if A=1,2,3,...,N. For different values of A, you would have to change this into:
A=1:N;
nthPerm=getfield(perms(A),{n,1:length(A)})

splitting a Matrix into column vectors and storing it in an array

My question has two parts:
Split a given matrix into its columns
These columns should be stored into an array
eg,
A = [1 3 5
3 5 7
4 5 7
6 8 9]
Now, I know the solution to the first part:
the columns are obtained via
tempCol = A(:,iter), where iter = 1:end
Regarding the second part of the problem, I would like to have (something like this, maybe a different indexing into arraySplit array), but one full column of A should be stored at a single index in splitArray:
arraySplit(1) = A(:,1)
arraySplit(2) = A(:,2)
and so on...
for the example matrix A,
arraySplit(1) should give me [ 1 3 4 6 ]'
arraySplit(2) should give me [ 3 5 5 8 ]'
I am getting the following error, when i try to assign the column vector to my array.
In an assignment A(I) = B, the number of elements in B and I must be the same.
I am doing the allocation and access of arraySplit wrongly, please help me out ...
Really it sounds like A is alread what you want--I can't imagine a scenario where you gain anything by splitting them up. But if you do, then your best bet is likely a cell array, ie.
C = cell(1,3);
for i=1:3
C{i} = A(:,i);
end
Edit: See #EitanT's comment below for a more elegant way to do this. Also accessing the vector uses the same syntax as setting it, e.g. v = C{2}; will put the second column of A into v.
In a Matlab array, each element must have the same type. In most cases, that is a float type. An your example A(:, 1) is a 4 by 1 array. If you assign it to, say, B(:, 2) then B(:, 1) must also be a 4 by 1 array.
One common error that may be biting you is that a 4 by 1 array and a 1 by 4 array are not the same thing. One is a column vector and one is a row vector. Try transposing A(:, 1) to get a 1 by 4 row array.
You could try something like the following:
A = [1 3 5;
3 5 7;
4 5 7;
6 8 9]
arraySplit = zeros(4,1,3);
for i =1:3
arraySplit(:,:,i) = A(:,i);
end
and then call arraySplit(:,:,1) to get the first vector, but that seems to be an unnecessary step, since you can readily do that by accessing the exact same values as A(:,1).