a=magic(5)
k=a,3
When I print k, it simply shows a.
m=size(a,3)
n=size(a,6)
when I print m and n, they print different values.
Anyone please explain what this function is?
On Octave 4.2.1
k=a,3
assigns the matrix a to the variable k, then, as a second instruction, prints on the CommandWindow the value 3.
The , (comma) is used in order to have two instruction on the same row.
An alterntive could be replacing the , with the ; which has the effect of suppressing the output on the CommandWindow of the assignment k=a
With respect to
m=size(a,3)
n=size(a,6)
the second parameter n the call to size specifies the dimension of the matrix (the first parameter) for which you want to know the size.
a is a two "dimensional" matrix of size (5 x 5) while the instruction size(a,3) looks for the size of the third dimension of a.
In a similar way, size(a,6) looks for the size of the a's sixth dimension. In these case, the a is considered as (5 x 5 x 1) and (5 x 5 x 1 x 1 x 1 x 1)
The return value, for is 1
This is the output in the CommandWondow:
>> a=magic(5)
a =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> k=a,3
k =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
ans = 3
>> m=size(a,3)
m = 1
>> n=size(a,6)
n = 1
In matlab / octave, there are three ways to terminate an expression (e.g. 1+2):
With a semicolon ;
With a comma ,
With a newline (i.e. pressing enter)
The first one (i.e. the semicolon) when used, evaluates the expression, but suppresses its output. The other two (i.e. the comma and the newline), both evaluate the statement and also display its result.
Why have both a comma and a newline? Because, with a comma, you can evaluate multiple expressions on the same line (and have all of them display their results).
Note: Given the fact that most people write their expressions in separate lines, the comma tends not to be used very much, so it is less known.
Examples:
octave:1> 1+2, 3+4
ans = 3
ans = 7
octave:2> 1+2; 3+4;
octave:3> 1+2; 3+4
ans = 7
octave:4> 1+2, 3+4;
ans = 3
octave:5> for i = 1:3; i; end % output in each iteration is suppressed
octave:6> for i = 1:3; i, end % whereas with a comma, output is not suppressed
i = 1
i = 2
i = 3
Therefore your statements:
a = magic(5)
k = a, 3
are essentially equivalent to
a = magic(5) % newline used: display value of a after assignment
k = a, % comma used, assign value of a to k, then display k
3 % newline used: displays the value '3' after pressing enter
Furthermore the size function doesn't do what you think it does. size(a,3) returns the size of array a in the 3rd dimension.
Related
data = reshape(1:21504,[256,4,21]);
data(:,5:4:end)
I test some indexes, such as:
data(:,5:4:end) ~= data(:,5:4:end,1)
data(:,5:4:end) ~= data(:,1,5:4:end)
So what is the meaning of data(:,5:4:end)?
I test some other indexes, such as:
data(1,1) == data(1,1,1)
data(1,1:3) == data(1,1:3,1)
And find some strange behavior ,such as data(1,1:10,1) returns error but data(1,1:10) is ok.
So What's happening here?
How can I understand this mechanism?
>> size(data)
ans =
256 4 21
data(1,1:10,1) selects column 1-10 from first row (all three dimensions are explicitly set), but there are only 4 columns. Therefore the error.
data(1,1:10), on the other hand, uses Linear indexing, which interpretes dimensions 2 and 3 as one long strung of values and selects its first 10 values.
Linear Indexing
What does this expression A(14) do?
When you index into the matrix A using only one subscript, MATLAB treats A as if its elements were strung out in a long column vector, by going down the columns consecutively, as in:
16
5
9
...
8
12
1
The expression A(14) simply extracts the 14th element of the implicit column vector. Indexing into a matrix with a single subscript in this way is often called linear indexing.
Here are the elements of the matrix A along with their linear indices:
matrix_with_linear_indices.gif
The linear index of each element is shown in the upper left.
From the diagram you can see that A(14) is the same as A(2,4).
The single subscript can be a vector containing more than one linear index, as in:
A([6 12 15])
ans =
11 15 12
Consider again the problem of extracting just the (2,1), (3,2), and (4,4) elements of A. You can use linear indexing to extract those elements:
A([2 7 16])
ans =
5 7 1
That's easy to see for this example, but how do you compute linear indices in general? MATLAB provides a function called sub2ind that converts from row and column subscripts to linear indices. You can use it to extract the desired elements this way:
idx = sub2ind(size(A), [2 3 4], [1 2 4])
ans =
2 7 16
A(idx)
ans =
5 7 1
(Copied from http://de.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html)
data(:, 5:4:end) will access all elements in the first dimension of data and starting from index 5 every 4th index until the last index in the second dimension of data. The syntax for this indexing technique can be explained like this:
data(startIndex:step:endIndex)
If data has more dimensions than you used for indexing, this will assume : for every dimension after that.
To sum up my question:
data=reshape(1:24,2,3,4)
data(:,:,1) =
1 3 5
2 4 6
data(:,:,2) =
7 9 11
8 10 12
data(:,:,3) =
13 15 17
14 16 18
data(:,:,4) =
19 21 23
20 22 24
Using this example you can know what Matlab doing:
data(:,1)
ans =
1
2
data(:,12)
ans =
23
24
data(:,[1,12])
ans =
1 23
2 24
data(:,5:4:end)
ans =
9 17
10 18
If you use data(:,13),it throws an error.
I have a matrix of 2d lets assume the values of the matrix
a =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
17 24 1 8 15
11 18 25 2 9
This matrix is going to be divided into three different matrices randomly let say
b =
17 24 1 8 15
23 5 7 14 16
c =
4 6 13 20 22
11 18 25 2 9
d =
10 12 19 21 3
17 24 1 8 15
How can i know the index of the vectors in matrix d for example in the original matrix a,note that the values of the matrix can be duplicated.
for example if i want to know the index of {10 12 19 21 3} in matrix a?
or the index of {17 24 1 8 15} in matrix a,but for this one should return only on index value?
I would appreciate it so much if you can help me with this. Thank you in advance
You can use ismember with the 'rows' option. For example:
tf = ismember(a, c, 'rows')
Should produce:
tf =
0
0
1
0
0
1
To get the indices of the rows, you can apply find on the result of ismember (note that it's redundant if you're planning to use this vector for matrix indexing). Here find(tf) return the vector [3; 6].
If you want to know the number of the row in matrix a that matches a single vector, you either use the method explained and apply find, or use the second output parameter of ismember. For example:
[tf, loc] = ismember(a, [10 12 19 21 3], 'rows')
returns loc = 4 for your example. Note that here a is the second parameter, so that the output variable loc would hold a meaningful result.
Handling floating-point numbers
If your data contains floating point numbers, The ismember approach is going to fail because floating-point comparisons are inaccurate. Here's a shorter variant of Amro's solution:
x = reshape(c', size(c, 2), 1, []);
tf = any(all(abs(bsxfun(#minus, a', x)) < eps), 3)';
Essentially this is a one-liner, but I've split it into two commands for clarity:
x is the target rows to be searched, concatenated along the third dimension.
bsxfun subtracts each row in turn from all rows of a, and the magnitude of the result is compared to some small threshold value (e.g eps). If all elements in a row fall below it, mark this row as "1".
It depends on how you build those divided matrices. For example:
a = magic(5);
d = a([2 1 2 3],:);
then the matching rows are obviously: 2 1 2 3
EDIT:
Let me expand on the idea of using ismember shown by #EitanT to handle floating-point comparisons:
tf = any(cell2mat(arrayfun(#(i) all(abs(bsxfun(#minus, a, d(i,:)))<1e-9,2), ...
1:size(d,1), 'UniformOutput',false)), 2)
not pretty but works :) This would be necessary for comparisons such as: 0.1*3 == 0.3
(basically it compares each row of d against all rows of a using an absolute difference)
Suppose I have:
>> X = magic(5)
X =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
How do I get i'th element from the second column?
I already figured that indices in (some?) collections in Octave are one-based, but I'm not sure if that holds for matrices, too.
See the index expressions section of the manual. To get the i'th element from second column:
X(i,2) # element 'i' from column 2
X(1:end,2) # the whole 2nd column
X(:,2) # same thing but shorter
x(:, [2 3]) # whole 2nd and 3rd column
Note that Octave is a language where array elements are in column-major order.
I'm sure there's an easy answer to this, but I'm not really sure what to search for. I have an array, M, of D dimensions, where D is constrained to be 1 <= D <= 5, and a vector of length D, X. I'd like to use D as an address within M and increment the value at that address, so if D were [1 2 3], I would want to increment M(1,2,3). I know I can do it like so:
if D == 1
M(X(1)) = M(X(1)) + 1;
end
if D == 2
M(X(1), X(2)) = M(X(1), X(2)) + 1;
end
But it's really ugly and I have to imagine there's a simpler, less clumsy way. Thanks!
You can use the function sub2ind to convert the address vector D to the corresponding dimensions in M. However, this would require that you store D as a cell and not a vector. The following example should help.
A=magic(5);%# just a test matrix
A=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
d={3,4};%we need the element at row 3, column 4
indx=sub2ind(size(A),d{:});%# get the index corresponding to the subscript 3,4
A(indx)
ans=
20
You can also directly index it into the matrix A as A(sub2ind(size(A),d{:})), without having to create a separate variable.
You can also use num2cell to convert the vector to a cell. This might be a better option, as you might want to store D as a vector for other purposes. So the corresponding line becomes
indx=sub2ind(size(A),num2cell(d));
I came across some MATLAB syntax with a colon that I don't fully understand.
First Question:
The expression: 0:pi/4:pi
results in the answer: 0 0.7854 1.5708 2.3562 3.1416
Why is this the case? I thought that colon operator is used as a quick way to refer to indices so that we don't have to write out the full list. (e.g. 1:3 -> 1 2 3)
Second Question:
Similar to above, say if I have a matrix X = [1 2 3 4 5 6 7 8 9]. How can I interpret the expression X(:,1:3)? Specifically, what does the colon operator without the left and right numbers mean?
Actually a:b generates a vector. You could use it as index only because the (...) accepts a list also, e.g.
octave-3.0.3:10> a = [1,4,7]
a =
1 4 7
octave-3.0.3:11> b = [1,4,9,16,25,36,49]
b =
1 4 9 16 25 36 49
octave-3.0.3:12> b(a) # gets [b(1), b(4), b(7)]
ans =
1 16 49
Now, the a:b:c syntax is equivalent to [a, a+b, a+2*b, ...] until c, e.g.
octave-3.0.3:15> 4:7:50
ans =
4 11 18 25 32 39 46
which explains what you get in 0:pi/4:pi.
A lone : selects the whole axes (row/column), e.g.
octave-3.0.3:16> a = [1,2,3;4,5,6;7,8,9]
a =
1 2 3
4 5 6
7 8 9
octave-3.0.3:17> a(:,1) # means a(1:3, 1)
ans =
1
4
7
octave-3.0.3:18> a(1,:) # means a(1, 1:3)
ans =
1 2 3
See the official MATLAB doc on colon (:) for detail.
My two pennies to KennyTM's answer.
Actually scalar and vector variables in MATLAB have 2 dimensions. Scalar has 1 row and 1 column, and vector has either 1 row or column. Just try size(X).
Colon (:) operator for indexing simply means all. Syntax X(:,1:3) means get all rows and columns from 1 to 3. Since your variable X has only 1 row, you will get first 3 values in this row.