Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
Anyone know how to start this problem? I mean, I understand what a hash does, but I have no idea what this quesiton is talking about.
Any ideas on how to go about this?
Given:
the hash function: h(x) = | 2x + 5 | mod M
a bucket array of capacity N
a set of objects with keys: 12, 44, 13, 88, 23, 94, 11, 39, 20, 16, 5 (to input from left to right)
4.a *[5 pts]***** Write the hash table where M=N=11 and collisions are handled using separate chaining.
4.b *[5 pts]***** Write the hash table where M=N=11 and collisions are handled using linear probing.
4.c *[5 pts]***** If M=11 can you find a value of N that generates no collisions hashing those keys?
Use the equation h(x) to find the hash value of each key. This is the location in the array where the value is stored. Since, this is clearly homework, I won't explain linear probing or separate chaining or 4c.
M is the size of the array that you put the values in.
N is the number of objects that you're hashing.
You have a function which given a number (x) determines where that should go in a table.
The table has a given size, N. In the case of parts a and b of this question N is 11.
M is 11 for questions a, b and c. M is just a value which is plugged into the given formula h(x) = (2x + 5) mod M
So given the number 12, h(12) = (2 * 12 + 5) mod 11 => 7. So the first result goes into bucket 7.
You then work your way through the rest of the numbers in turn, working out what h(x) is for each.
However, when you come to a collision (i.e. a scenario where the bucket which the number should go in has already been filled by a previous number) you need to select a different bucket for it. Which bucked you select will depend on your overflow strategy.
in question A your overflow strategy is separate chaining
in question B your overflow strategy is linear probing
if you're unfamiliar with these methods, watch these:
Separate Chaining: http://www.youtube.com/watch?v=IDqTof09ufg
Linear Probing: http://www.youtube.com/watch?v=g0n0Ep18DJc
If C is taken as read I assume it means that take the numbers from the start of the list until you hit the first collision; however many numbers you already have in your bucket is the value for N.
However, I believe question C I believe is a misprint. I think it wants you to find a value for M which would allow the given list of numbers to all be assigned to a unique bucket (i.e. no collisions / no overflow strategy).
Start by computing the hash of each key.
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
How can I retrieve the last element of a vector only, provided that the length of that vector is unknown?
Use the special end keyword:
lastelement = myvector(end);
If the vector is called A, just use A(end).
In this case, use end, like #nispio and #David answered.
But it seems you think that not knowing the length can be a problem, but nope. That's because you can use length(v) if v is a column or row vector, or size(M) if M is a matrix.
Then, to get the last element of your vector, you could use (not recommended):
v(length(v)) if v is a row or a column vector
v(size(v,1)) if v is a column vector
v(size(v,2)) if v is a row vector
But if you use one of them, MATLAB will warn you:
The operation or expression <Indexing> has no evident effect.
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.
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
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).