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

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).

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.

Why is Scala's Range a data structure instead of a function? [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 6 years ago.
Improve this question
I come from a background in languages such as Python and R, both of which have range() functions which return that language's common collection type (Python's range() returns a list, while R's range() returns a vector).
So it's pretty surprising to me that Scala offers Range, not as a function, but as a data type. Even more surprising, Range is not simply an alias for a more common collections type such as List or Vector, but is an independent data type of its own!
Could someone please explain the reasoning what part of Scala's design philosophy led to Range being a unique data type?
Differences between Range and List
Range and List are two different concepts, so they are implemented as two different classes in Scala. List is particular implementation of a sequence of values. Those can be any values in any order. On the other hand Range is an implementation of ordered sequence of Ints, that can be described by three Ints: start, end and step (starting value, end value and a step size).
List(1, 4, 7, 10, 13) represent the same thing as Range(1, 14, 3), but differently. The main advantage of Range over List used as range, is that
Range is more memory efficient. List used as range keeps in memory all integers in that range, where Range keeps in memory only the values of start, end and step.
Ranges are represented in constant space, because they can be defined
by just three numbers: their start, their end, and the stepping value.
Because of this representation, most operations on ranges are
extremely fast. [source]
You can convert Range to List by calling toList on it (or toVector/toArray to convert it to corresponding collection type).
Creating Range by calling method
In Scala the preferred way of creating Ranges is to call to, until and by methods (defined in scala.runtime.RichInt and Range classes):
val r1: Range = 1 to 10 // range from 1 to 10 including 1 and 10 with step size 1
val r2: Range = 1 until 10 // range from 1 to 10 including 1 but not 10 with step size 1
val r3: Range = 1 to 10 by 3 // range from 1 to 10 including 1 and 10 with step size 3
Why not have a function that returns a list
Like Python 3's range (and Python 2's xrange), Scala's Range is an iterable structure that only generates the elements of the range on-demand and does not actually store them in memory. This is preferable to lists for big ranges as storing those in memory will consume a lot of memory, often for no benefit as most uses of ranges do not require the elements to be stored in memory. That's also why Python introduced xrange and then later replaced the old range with it.
So why not have a function the returns a Seq or the like
You can ask a Range what its starting value, it's end and it's step-size are. You couldn't do that if it were just a Seq.

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

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.

Predicting an overflow when counting combinations

We have the following formula for determining how many combinations C we can pick of size k out of a set of n:
I have written an algorithm which will always give an answer if, of course, the answer falls within the range of the datatype (ulong, in my case), by factorising and cancelling terms on the numerator and denominator during evaluation.
Even though it's quite fast to try to compute C and detect an overflow if the result is too large, it would be better if I could put n and k into a preliminary function which estimates whether the answer will be larger than what ulong can hold. It doesn't have to be exact. If it estimates that a given n and k will not overflow but it does, that's fine - but it should never say this it will overflow if it won't. Ideally this function should be very fast otherwise there is no point in having it - I may as well try and compute C directly and let it overflow.
I was plotting the curve of the nCk for various n's as a function of k to see if I can find a curve which grows at least as fast as C(n, k) but doesn't diverge too far in the range I'm interested in (0..2^64-1) and is computationally easy to evaluate.
I didn't have any luck. Any ideas?
Without seeing the actual code for your algorithm, I can't give you a 100% solution, but your best bet is to develop a heuristic function. By simply finding the smallest value of r for which the final answer to nCr overflows for a variety of n values, you should then be able to analyze the relationship between something like n and the ratio between n and r (n/r), and find a quick to calculate function which would let you know if overflow would occur via regression.
I found that for any n < 68, you should never overflow on the final answer, as 67C33 = 67C34 ~ 1.42x1019 is the largest possible answer, and a ulong holds ~1.84x1019. Similarly, when n > 5000, any r > 5 or n-r < n-5 will certainly overflow. You can tune these cutoffs to your liking, and for all the n values in between them, just calculate n/r and use the regression formula to decide if it will overflow or not.
This might be too much work, but it should at least get you started on the right path.