This question already has answers here:
Find vector elements matching two conditions in Matlab
(3 answers)
Closed 9 years ago.
How to find the value in a matrix with certain condition. For example,
a=[-3.14,2.12,-5,3,6,7];
b=find(a>0)
this will return the indices of matrix with that ">0" condition, which is b= 2 4 5 6.
do we have any solution for find the actual value in a matrix under that condition, such as returning b= 2.12 3 6 7 ?
you can even skip the find part:
whatyouwant = a(a>0);
That's called logical indexing in Matlab...
You could do the following
a = [-3.14,2.12,-5,3,6,7];
b = find(a>0)
c = a(b)
The c would then be the selected values based on the indices in b.
Hope it helps!
Related
This question already has answers here:
Return Unique Element with a Tolerance
(7 answers)
Closed 5 years ago.
When multiplying matricies what is the difference when there is a dot there? Can this give different results
You can just use the built-in Matlab function uniquetol to obtain the unique values up to some tolerance in the array and ask the length of the returned array.
Example
A = [1+1e-11 2 3 4 1 2 3]; % generate an array with 4 unique values except for some tolerance
length(uniquetol(A, 1e-10)) % will return 4
This question already has answers here:
Combinations from a given set without repetition
(2 answers)
Closed 7 years ago.
I want to access to row i of A=nchoosek(1:m,n). This command of MATLAB is very time consuming, specially for large m. So, I do not want to construct whole of A. I want to build just rowi of A.
Although, it seems that my question is duplicate of "Combinations from a given set without repetition", but they are different.
That answer did not cover different columns. It is just get acceptable results for A = nchoosek(M,2). I want to find A (i,:), where A= nchoosek(1:m,n), for given i, m, and n.
This answer answers the original version of the question, not the updated version
This is exactly what nchoosek does, when you input a vector.
nchoosek([1:n],m)
.
>> m=2
m =
2
>> n=5
n =
5
>> nchoosek([1:n],m)
ans =
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Understanding why I can't use index on the result of the size function on a matrix in matlab
(1 answer)
Closed 8 years ago.
Doesn't Matlab allow chaining of matrix operations with indexing?
For eg.:
a = [1 2; 3 4];
exp(a)(:)
throws the error
Error: ()-indexing must appear last in an index expression.
It seems like this is something I would have expected Matlab to have or is there a different way to do this?
You don't need the indexing colon. The below should work.
a = [1 2; 3 4];
exp(a);
This question already has an answer here:
matlab based program where output comprises of sum of rows of input
(1 answer)
Closed 8 years ago.
I am trying to implement a vectorised solution in matlab for adding all elements above the current element in a vector. For eg.
I have a vector a as follows
a =
1
2
3
4
I would like a vector b like
b =
1
3
6
10
I know this can be done very easily using a loop but I was wondering if there are indexing options which can allow me to do the same in matlab/ octave?
You can use the Cumulative Summation function (cumsum):
b = cumsum(a)
This question already has answers here:
MATLAB - extracting rows of a matrix
(5 answers)
Closed 10 years ago.
I have a Nx2 vector, each row in the vector is a coordinate in a matrix.
For example: vector that call Path look like this:
Path=[1 2;
3 4;
5 6;
7 8;];
My question is how can I access the vector to take my x and y coordinates?
If I write Path(1) the answer is 1, and for Path(2) the answer is 3, But I want to take the pairs 1 2, then 3 4 etc..
Can I do it in a loop?
thanks!
This will give you every row one by one.
for i=1:size(Path,1)
Path(i,:)
end
If you just want to plot the path, try:
plot(Path(:,1),Path(:,2))