Is the first dimension always the Y-dimension (vertical one) while the second dimension refers to the X-dimension (horizontal one)? Is there any exceptions?
There are no exceptions.
The only subtlety is that if you only specify 1 index (eg x(10)), that refers to the 10th element overall, not the 10th element in dimension 1. So you have a size(x)=[2 10], then x(10) == x(2,5).
There are two things you need to keep in mind:
MATLAB operates always along the first non-singleton dimension
In a matrix, the first dimension is along rows and the second is along columns
Within this principles falls array indexing.
Another example, if you have a vector (abusing notation):
sum(reshape(1:3,[1,1,3])) == sum(1:3) == sum((1:3)')
if you have a matrix:
sum([1 2; 3 4]) ~= sum([1 2; 3 4],2)
i.e. sum along rows (also called column-wise) is different from the sum along columns (also called row-wise).
Related
I have a 3 by 3 by 2 by 3 array X containing 1s or 0s. Picture this as a row of three 3 by 3 matrices along a row and another such 'layer' behind them (from the '2').
I want to find the positions in X of the 0s in the first layer and second layer separately. I'm not really sure how to do this with find, but heuristically something like:
A = find(X == 0 & 3rd index of X is 1)
B = find(X == 0 & 3rd index of X is 2)
EDIT
I just realised my attempt to simplify my actual question made it misleading. The array X actually has -1's, 1's and -2's and I want to find the -2's. They're not meant to be logical operators. Also I would prefer any operation proposed to be as fast as possible as this will be part of a recursive backtracking algorithm.
solution using logical indexing
I recommend to use logical indexing instead of find.
This gives you all indices where X is 1
value_you_want=-2
C=X==value_you_want;
Now you want only parts of these indices in A and B, first initialize A and B with false of the same size as C:
A=false(size(C));
B=A;
And finally copy the slice you want to each of these matrices:
A(:,:,1,:)=C(:,:,1,:);
B(:,:,2,:)=C(:,:,2,:);
If you really want your numeric indices, use find(A) and find(B)
Alternative solution using linear indices and find
%get all indices
C=find(X==value_you_want)
%convert linear indices to subscript indices, only use third dimension
[~,~,S,~]=ind2sub(size(X),find(X==0));
%Use S to split C
A=C(S==1);
B=C(S==2);
Generally use find(condition) to return linear indices in the array satisfying condition.
A = find(A(:,:,1,:)<1)
B = find(A(:,:,2,:)<1)
I created a matrix with two columns, using two vectors, f and PS, that I already had:
M = [f PS]; %81x2 matrix
And then I sorted the matrix with respect to "PS" (second column), in order to have the maximum values of "PS" at the last positions of the vector:
M1=sortrows(M,2); %81x2 matrix
And I got something like this:
f...PS
5...83
10...136
3...357
1...985
Since now I assured the last two values of "PS" are indeed the maximum values, the information I want to give to the user is 1 and 3 (f values corresponding to the 2 maximum values of PS, which are at the bottom).
How can I do this?
You can use the end index: M1(end,1) and M1(end-1,1) should contain 1 and 3, respectively.
Best,
i have two matrices
r=10,000x2
q=10,000x2
i have to find out those rows of q which are one value or both values(as it is a two column matrix) different then r and allocate them in another matrix, right now i am trying this.i cannot use isequal because i want to know those rows
which are not equal this code gives me the individual elements not the complete rows different
can anyone help please
if r(:,:)~=q(:,:)
IN= find(registeredPts(:,:)~=q(:,:))
end
You can probably do this using ismember. Is this what you want? Here you get the values from q in rows that are different from r.
q=[1,2;3,4;5,6]
r=[1,2;3,5;5,6]
x = q(sum(ismember(q,r),2) < 2,:)
x =
3 4
What this do:
ismember creates an array with 1's in the positions where q == r, and 0 in the remaining positions. sum(.., 2) takes the column sum of each of these rows. If the sum is less than 2, that row is included in the new array.
Update
If the values might differ some due to floating point arithmetic, check out ismemberf from the file exchange. I haven't tested it myself, but it looks good.
Suppose I have a vector B=[1 1 2 2] and A=[5 6 7 4] in the form of B says the numbers in the A that are need to be summed up. That is we need to sum 5 and 6 as the first entry of the result array and sum 7 and 4 as the second entry. If B is [1 2 1 2] then first element of the result is 5+7 and second element is 6+4.
How could I do it in Matlab in generic sense?
A fexible and general approach would be to use accumarray().
accumarray(B',A')
The function accumulates the values in A into the positions specified by B.
Since the documentation is not simple to understand I will summarize why it is flexible. You can:
choose your accumulating function (sum by default)
specify the positions as a set of coordinates for accumulation into ND arrays
preset the dimension of the accumulated array (by default it expands to max position)
pad with custom values the non accumulated positions (pads with 0 by default)
set the accumulated array to sparse, thus potential avoiding out of memory
[sum(A(1:2:end));sum(A(2:2:end))]
I am a new in MATLAB and I have a problem understanding the function size
in this statement: for i=1:size(scale,2) WHERE scale can be any integer number .e.g scale=5.
I found that in MATLAB help size(A,1) returns the number of rows of A , and
size(A,2) returns the number of columns of A.
Now I'm really confused as to what is the functionality of (size).
As you know, matlab deals mainly with matrices. So, the size function gives you the dimension of a matrix depending on how you use it. For example:
1. If you say size(A), it will give you a vector of size 2 of which the first entry is the number of rows in A and the second entry is the number of columns in A.
2. If you call size(A, 1), size will return a scalar equal to the number of rows in A.
3. If you call size(A, 2), size will return a scalar equal to the number of columns in A.
A scalar like scale in your example is considered as a vector of size 1 by 1. So, size(scale, 2) will return 1, I believe.
Hope this clarifies.
The Linear Algebra operations in Matlab/octave by default follow Row-Column order (ie they are row major by default); so if A is a matrix of size 3x2 (3 rows and 2 columns), we can use size to determine the order of matrix/vector
size(A) will return 3 2 (the first entry representing no.of rows & the second one is no.of columns). Similarly,
size(A,1) returns 3 (1 here represents the no. of rows and A has 3 rows)
size(A,2) returns 2 (2 here represents the no. of columns and A has 2 columns)