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

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.

Related

Basics on Matlab function Unique [closed]

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.

How to compare two matrices on MatLab? [closed]

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
Given a matrix A and a matrix B, how do I compare them element by element so the program returns a third matrix C that shows:
- If the element in A is larger than the one in B, the element in C should be 1.
- If the element in A is smaller than in B, the element in C should be -1.
- If the elements of both matrices are equal, the element in C should be 0.
Hope you can help!
C=zeros(size(A));
C(A>B) = 1;
C(A<B) = -1;
Note that it's never a good idea to do an equality test on floating point numbers.

Returning the last element of a vector with an unknown length [closed]

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.

getting occurrence of alphabets in a string, in Matlab [closed]

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
i have to do huffman coding on a message read from a file,in Matlab.For that i have to find the probability occurrence of each alphabet in that message.Using that frequency i have to do huffman encoding.Can you please specify how to read a message from the file and store it as a string for the same purpose..Can anyone help me to solve this
What you need is a histogram count:
counts = histc(lower(x), 'a':'z');
where the output count contains the number of occurences for each letter in the message string x. For instance, the first element count(1) corresponds to the number of occurrences of a, count(2) corresponds to the number of occurrences of b, etc...
Also note that this x is converted to lowercase o make the counting case insensitive.

hash function confusion [closed]

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.