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
Related
This question already has answers here:
Run-length decoding in MATLAB
(4 answers)
Closed 6 years ago.
I have a variable distr=[0 3 1 0 2];, and I have a variable full which should contrain distr(i) times i, for all i.
In this example, i want:
full=[2 2 2 3 5 5];
because distr(2)=3, therefore 3x 2, and so on.
Of course I can do it in a for-loop:
full=zeros([1,sum(distr)]);
cc=1;
for i=1:length(distr)
curr=distr(i);
full(cc:cc+curr-1)=i*ones([1,curr]);
cc=cc+curr;
end
but that is very slow. Do you know of a fast way, using MATLAB's awesome array-oriented style? Thanks!
Not sure, but maybe this will work. I can't check it since I currently don't have MATLAB:
full_tmp = arrayfun(#(i,n) i*ones(1,n),1:length(distr),distr,'uniformoutput',false);
full = cat(2,full_tmp{:});
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:
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!
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))