Basics on Matlab function Unique [closed] - matlab

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm studying the function unique, which looks simple at a first sight but I don't understand some properties.
I created a matrix A and tried to analyze the outputs:
A=[5 2 4 5;
1 1 3 4;
6 1 2 3]
[C0,IA0,IC0]=unique(A)
[Cr,IAr,ICr]=unique(A,'rows')
[Cf,IAf,ICf]=unique(A,'first')
In C0 the logic of the output is "create a vector in which there are values that appears at least one time"
But I don't know the meaning of IA0 and IC0. I just know the relation that C=A(IA0) and A=C(IC0). Are these 2 output created only to satisfy this two relation? So why should I be intersted in their outputs?
In Cr ('rows' example) the logic of the output is "give me back the rows of the original matrix A but sorted ascending. Also, if you find at least two or more rows that repeat with same values and order, show that row only once in Cr output"
The logic of IAr is very intuitive: "give me back the index that must follow the Cr output to order the rows." So in my example gives me back a vector like IAr=[2;1;3]. Thus the second row of the original matrix A must be the first in the Cr output, the first row in A matrix must be the second in Cr...
But I still don't understand the output ICr
In Cf ('first' example) gives me back the same output as C0. And it's not really clear to me how to use properly this function.
Can anyone gives me a simple explanation about how this function works?
Are there any simple practical examples in which I can take advantage of these other outputs?

Matlab is a generic tool. So asking "why" some functions give extra output just because it might be useful to someone for some reason is difficult to answer.
IA0 gives the indices of the last elements that might have been chosen by unique. IC0 contains indices to replicate A using only the unique elements. Why do they exist? You may not only be interested in unique values, but also where to find them in A.
You are misinterpreting how rows works. It simply treats the rows of your matrix as atomic and returns the unique rows. To get a better idea of how it works, run A(3,:) = A(2,:) before calling unique. Then the idea behind IA0 and IC0 is the same as in the previous case, except now with rows.
The first option only changes how Matlab chooses the indices of IA0. Again, the reason behind this is that Matlab is a generic tool. You might be interested in finding the latest repeated occurence of each value or in the first. It depends on what you want to do.

Related

Using Matlab to find output at specified input values

spacing_Pin = transpose(-27:0.0001:2);
thetah_2nd = Phi_intrp3(ismembertol(spacing_Pin,P_in2nd));
With this code, I want to evaluate Phi_intrp3at indices where spacing_Pinis equal to P_in2nd
I know I have asked similar questions before. And I have got some really helpful answers already. But in this case they do not seem to apply. P_in2ndhas only 40 entries, whereas spacing_Pinhas far more. Therefore I cannot consider the absolute value of the difference of spacing_Pinand P_in2ndto find out where they are closest to equal.
so P_in2ndhas values between -25.9747 and -0.0147. The decimals have 4 digits after the dot, but these are sometimes rounded by Matlab (format short). That's the catch, I think, P_in2nd is not found in spacing_Pin. The result is an empty matrix.
Here's the first 5 entries of P_in2nd:
-25,9747431735299
-24,9747431735299
-23,9947431735299
-23,0047431735299
-22,0047431735299
Now, I want to evaluate ¸Phi_intrp3at these values. For this purpose I can change spacing_Pin, but not P_in2nd. For example, when I search for the first entry of P_in2ndin spacing_Pin, I find that entry 10254 = -25,9747000000000. So I want to evaluate Phi_intrp3at this input entry.
Is there a way of doing this?

To find an element with nearest lower and greater value in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have two sorted matrices A and B. For all values in column 1 of A, how do I find nearest lower and greater value in matrice B? (no treshold)
I would use interp1, but in the opposite way it's normally used. Consider your B matrix a look-up table. You're trying to look up the index of an element given it's value. For example:
% Sample data
B = sort(rand(10,1));
A = sort(rand(5,1));
idx = interp1(B, 1:size(B), A, 'linear', 'extrap');
idx will be a double-precision value that shows the location of each element of A in B. 2.2, for instance, says that the value is between element 2 and element 3. In fact, it's 20% of the way from element 2 to element 3. So floor(idx) is the lower element, and ceil(idx) is the higher.
Caveats: duplicate elements in B will create a problem. And the edge conditions might be messy. You'll have to work those out yourself. See what happens with an A element that's outside the range of B.

How can i increase speed of for loop in matlab? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to read the value of pixcels in image result to compare this value with some
I use to for loop
function GrdImg= GrdLbp(VarImg,mapping,LbpImg)
tic
p=mapping.samples;
[Ysize,Xsize]=size(result);
GImg=zeros(Ysize,Xsize);
temp=[];
cnt=1;
for n=0:p-1
temp(cnt)=2^n;
temp(cnt+1)=(2^p)-1-(2^n);
cnt=cnt+2;
end
for i=1:Ysize
i
for j=1:Xsize
if isempty(find(result(i,j)==temp(:,:)))==1
GImg(i,j)=sqrtm(Vresult(i,j));
end
end
end
but it works too slow, Could you help me what can I use instead of for loop?
Thanks a lot
You didn't really give enough information to answer your question - since, as was stated in the comments, you aren't doing anything with the values in the loop right now. So let me give you a few ideas:
1) To compare all the pixels with a fixed value, and return the index of all pixels greater than 90% of the maximum:
threshold = 0.9 * max(myImage(:));
prettyBigPixels = find(myImage > threshold);
2) To set all pixels < 5% of max to zero:
threshold = 0.05 * max(myImage(:));
myImage(myImage < threshold) = 0;
In the first case, the find command returns all the indices (note - you can access a 2D matrix of MxN with a single index that goes from 1 to M*N). You can use ind2sub to convert to the individual i, j coefficients if you want to.
In the second case, putting (myImage < threshold) as the index of the matrix is called logical indexing - it is very fast, and will access only those elements that meet the criterion.
If you let us know what you're actually doing with the values found we can speed things up more; because right now, the net result of your code is that when the loop is finished, your value Temp is equal to the last element - and since you did nothing in the loop we can rewrite the whole thing as
Temp = pixel(end);
EDIT Now that you show what you are doing in your inner loop, we can optimize more. Behzad already showed how to speed up the computation of the vector temp - nothing to add there, it's the right way to do it. As for the two nested loops, which are likely the place where most time is spent, you can find all the pixels you are interested in with a single line:
pixelsOfInterest = find(~ismember(result(:), temp(:)));
This will find the index of pixels in result that do not occur in temp. You can then do
GImg(pixelsOfInterest) = sqrt(result(pixelsOfInterest));
These two lines together should replace the functionality of everything in your code from for i=1:Ysize to the last end. Note - your variables seem to be uninitialized, and change names - sometimes it's result, sometimes it's Vresult. I am not trying to debug that; just giving you a fast implementation of your inner loop.
As of question completely edited I answer new rather than edit my former answer, by the way.
You can improve your code in some ways:
1. instead of :
for n=0:p-1
temp(cnt)=2^n;
temp(cnt+1)=(2^p)-1-(2^n);
cnt=cnt+2;
end
use this one:
temp=zeros(1,2*p);
n=0:p-1;
temp(1:2:2*p)=2.^n; %//for odd elements
temp(2:2:2*p)=2^p-1-2.^n; %//for even elements (i supposed p>1)
2.when code is ready for calculating and not for debugging or other times, NEVER make some variables to print on screen because it makes too long time (in cpu cycles) to run. In your code there are some variables like i that prints on screen. remove them or end up them by ;.
3.You can use temp(:) in last rows because temp is one-dimensional
4.Different functions are for different types of variables. in this code you can use sqrt() instead of sqrtm(). it may be slightly faster.
5. The big problem in this code is in your last comparison, if non elemnt of temp matrix is not equal with result specific element then do something! its hard to make this part improved unless knowing the real aim of the code! you may be solve the problem in other algorithm that has completely different code. But if there is no way, so use it in this way (nested loops) Good Luck!
It seems your image is grayscle or monocolor , because Temp=pixel(i,j) gives a number not 3-numbers by the way.
Your question has not more explanation so I think in three type of numbers that you are comparison with.
compare with a constant number
compare with a series of numbers
compare with a two dimensional matrix of numbers
If first or third one is your need, solution is very easy (absolutely in third one, size of matrix must be equal to pixel size)
Comparison with a number (c is number or two-dimensional array)
comp=pixel - c;
But if second one is your need, you can first reshape pixel to one-dimensional matrix then compare it with the series of number s (absolutely length of this serie must be equal to product of pixel rows number and columns number; you can re-reshape pixel matrix after comparison to primary two dimensional matrix.
Comparison with a number serie s
pixel_temp = reshape(pixel,1,[]);
comp = pixel_temp - s;
pixel_compared = reshape(pixel_temp,size(pixel,1),size(pixel,2)); % to re-reshape to primary size

What are the pros and cons of starting indices at 1 or another value [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
When I have to work on a portion of an array (elements n to n+m of array A), I never know arrays that I produce from this portion of array A (lets call it B) should start at 1 or n. I could either make B 1) range from elements 1 to m-n or 2) range from n to n+m. On a couple of occasions bugs have been produced from me getting this confused.
If memory is a constraint, then 2) wastes elements 1 to n. On the other hand, it is harder to process A and B together if I do 1) and end up needing to use different indices.
What are the benefits of each method when programming in MatLab?
There are benefits to always start array indexes with zero. While it's a matter of convention, it is a convention that in my experience minimizes error, especially when used in conjunction with closed-below, open-above intervals like [a, b). The length of such an interval is b - a. Since no 1s are involved in the expression, I can't forget to put them in. The interval [0, length) is the whole array: again, no need for 1s. Offsets like [x, x + sublen) also don't need any 1s. With 1-based indexing, combining offsets like x1, x2, x3 requires careful handling of 1s.
While some of these features can be obtained in 1-based indexing with closed-above intervals, not all of them can. When an entire framework adopts the zero-based index, closed-below, open-above convention, like most of C, Python, Java, etc., then you can happily avoid thinking about missing + 1 and - 1 errors in all of your function calls, which is a big help.
I remember having the choice to use any base for an index in Fortran (0, 1, -5, whatever). In the few cases where it was helpful (usually for making indexes symmetric around 0), the same effect could have been achieved by wrapping the array retreival in a function call. Since those cases almost always involved modelling a continuous variable by a grid, I often wanted to make the grid spacing different from 1 and interpolate between the points, too, and for that I absolutely needed to wrap it in a function call anyway.
I didn't know that Matlab gave you a choice (you're talking about Matlab, right?), but it would make sense, given Matlab's relationship to Fortran. The above argument has nothing to do with optimization, but if I'm understanding you right and Matlab fills in unused indexes with some kind of placeholder, then zero-based indexing would avoid that, too (as well as bugs associated with unintentionally using the placeholder as though it were real).

Find the m-th smallest number in Matlab? [duplicate]

This question already has answers here:
How to find the index of the n smallest elements in a vector
(2 answers)
Closed 9 years ago.
Is there an efficient way to find the m-th smallest number in a vector of length n in Matlab? Do I have to use sort() function? Thanks and regards!
You don't need to sort the list of numbers to find the mth smallest number. The mth smallest number can be found out in linear time. i.e. if there are n elements in your array you can get a solution in O(n) time by using the selection algorithm and median of median algorithm.
The link is to the Wikipedia article,
http://en.wikipedia.org/wiki/Selection_algorithm#Linear_general_selection_algorithm_-_Median_of_Medians_algorithm
Edit 2: As Eitan pointed the first part of the answer doesn't address the question of finding the smallest m-th value but regarding the m-th element after the min value. The rest of the answer remains... +1 for Eitan's sharpness.
While sort is probably very efficient to begin with, you can try to see whether a find will be better. For example:
id=find(X>min(X),m,'first');
id(end) % is the index of the smallest m-th element in X
the function find has added functionality that lets you find the 'first' or 'last' elements that meet some criterion. For example, if you want to find the first n elements in array X less than a value y, use find(X<y,n,'first')
This operation stops as soon as the first element meeting the condition is encountered, which can result in significant time savings if the array is large and the value you find happens to be far from the end.
I'd also like to recap what #woodchips said already in this SO discussion that is somewhat relevant to your question:
The best way to speed up basic built-in algorithms such as sort is to get a faster hardware. It will speed everything else up too. MATLAB is already doing that in an efficient manner, using an optimized code internally. Saying this, maybe a GPU add-on can improve this too...
Edit:
For what it's worth, adding to Muster's comment, there is a FEX file called nth_element that is a MEX wrap of C++ that will get a solution in O(n) time for what you need. (similar to what #DDD pointed to)
As alternative solution, you may follow this way:
A = randi(100,4000,1);
A = sort(A,'ascend');
m = 5; % the 5 smallest numbers in array A
B = A(1:5);
I hope this helps.