What kind of sorting algorithm is this? (Code in matlab) - matlab

I have a discussion with a teacher. He argues that the following algorithm corresponds to the bubble sort but I insist that it is not. Who is right?
clc
clear
a=[0.2 4.333 1/3 5 7]
n=length(a)
for j=n:-1:1
for i=1:j-1
if a(j)>a(i)
else
c=a(i);
a(i)=a(j);
a(j)=c;
a
end
end
end

This doesn't look like bubble sort as I understand it. It starts from the final element, and compares it to each other element, swapping with the final element until the entire array has been run through, confirming that the largest number is at the end. Bubble sort compares numbers in adjacent pairs.

Related

slicing assignment with negative index

I am having some problems regarding slicing assignment:
As i understand that general syntax of slicing is l[start:stop:step]
when we use positive step then we transverse forward and when we use negative step we transverse backward:
l=[1,2,3,4]
l[3:1:1]=[5]
when i use the above assignment then it inserts the element 5 at the index 3 like insert operation
but when i use
l[-3:-1:-1]=[5]
then it shows me value error....
i m totally confused..
please explain it.
Assuming you are asking about slices in Python,
the 'step' part will make the slice an extended slice.
Assigning to extended slices is only possible if the list on the right
hand side is of the same size as the extended slice.
see
https://docs.python.org/2.3/whatsnew/section-slices.html
So the confusing thing actually is that your l[3:1:1] = [5] does not raise
a ValueError, because the left and right size differ (0 and 1; note
that both your l[3:1:1] and l[-3:-1:-1] evaluate to empty lists).
I think that can be explained by the fact that a step of 1 is no different
from the original slice syntax [start:end], and may therefore be handled
as a normal slice.
If your goal is inserting, just don't use the step.

(matlab matrix operation), Is it possible to get a group of value from matrix without loop?

I'm currently working on implementing a gradient check function in which it requires to get certain index values from the result matrix. Could someone tell me how to get a group of values from the matrix?
To be specific, for a result matrx res with size M x N, I'll need to get element res(3,1), res(4,2), res(1,3), res(2,4)...
In my case, M is dimension and N is batch size and there's a label array whose size is 1xbatch_size, [3 4 1 2...]. So the desired values are res(label(:),1:batch_size). Since I'm trying to practice vectorization programming and it's better not using loop. Could someone tell me how to get a group of value without a iteration?
Cheers.
--------------------------UPDATE----------------------------------------------
The only idea I found is firstly building a 'mask matrix' then use the original result matrix to do element wise multiplication (technically called 'Hadamard product', see in wiki). After that just get non-zero element out and do the sum operation, the code in matlab should look like:
temp=Mask.*res;
desired_res=temp(temp~=0); %Note: the temp(temp~=0) extract non-zero elements in a 'column' fashion: it searches temp matrix column by column then put the non-zero number into container 'desired_res'.
In my case, what I wanna do next is simply sum(desired_res) so I don't need to consider the order of those non-zero elements in 'desired_res'.
Based on this idea above, creating mask matrix is the key aim. There are two methods to do this job.
Codes are shown below. In my case, use accumarray function to add '1' in certain location (which are stored in matrix 'subs') and add '0' to other space. This will give you a mask matrix size [rwo column]. The usage of full(sparse()) is similar. I made some comparisons on those two methods (repeat around 10 times), turns out full(sparse) is faster and their time costs magnitude is 10^-4. So small difference but in a large scale experiments, this matters. One benefit of using accumarray is that it could define the matrix size while full(sparse()) cannot. The full(sparse(subs, 1)) would create matrix with size [max(subs(:,1)), max(subs(:,2))]. Since in my case, this is sufficient for my requirement and I only know few of their usage. If you find out more, please share with us. Thanks.
The detailed description of those two functions could be found on matlab's official website. accumarray and full, sparse.
% assume we have a label vector
test_labels=ones(10000,1);
% method one, accumarray(subs,1,[row column])
tic
subs=zeros(10000,2);
subs(:,1)=test_labels;
subs(:,2)=1:10000;
k1=accumarray(subs,1,[10, 10000]);
t1=toc % to compare with method two to check which one is faster
%method two: full(sparse(),1)
tic
k2=full(sparse(test_labels,1:10000,1));
t2=toc

shuffling the string code in any language

I am trying to code a program to shuffle the dna sequence as much as
possible to destroy the order in the sequence. i have written matlab
code but its too slow. Also i was looking into hamming distance
measure or levenstein measure, also how can i incorporate those measure to
ensure proper shuffling. the rules I followed in shuffling
rule 1: the ith residue should not be near i-1,i-2,i-3,i+1,i+2,i+3
rule 2: in next arrangement i's new position and old position must be at 20 place difference. i.e. if A had 1st position in the string in
shuffled string it must be more than equal to 21st position.
function seq=shuffling(str)
len=length(str);
t1=0.4;
seqlen=1:len;
if(len>150)
t1=0.90;
elseif(len>=100)
t1=0.7;
end
while 1
shufseq=randperm(len);
temp1=diff([seqlen;shufseq]);%differences between order indices of original and shuffled arrangement
if(isempty(find(temp1==0)) && isempty(find(diff(shufseq)==1|diff(shufseq)==2 |diff(shufseq)==3 |diff(shufseq)==4 |diff(shufseq)==-1|diff(shufseq)==-2 |diff(shufseq)==-3 |diff(shufseq)==-4)))% rule 1
if((length(find(temp1>20|temp1<-20))/len)>t1)%rule 2 if ratio of (counts of arrangements/length of the string) should be more than one after certain length threshhold(=t1)
break
else
continue
end
else
continue
end
end
seq=str(shufseq);
i came up with one alternative. i.e. knowing the composition or counts of unique alphabets in the string. then choosing randomly among these alphabets and reducing their count by 1 in each iteration. this iteration is over the length of the sequence.
function seq=newshuffle(str)
%#codegen
len=length(str);
seq=[];
ndict= ['A';'C';'G';'T'];
ncomp=struct2array(count(str))';
for l=1:len
while 1
x=randi(4,1,1);
if ncomp(x)~=0
break;
end
end
seq=[seq,ndict(x)];
ncomp(x)=ncomp(x)-1;
end
end

vector of variable length vectors in MATLAB

I want to sum up several vectors of different size in an array. Each time one of the vectors drops out of my program, I want to append it to my array. Like this:
array = [array, vector];
In the end I want to let this array be the output of a function. But it gives me wrong results. Is this possible with MATLAB?
Thanks and kind regards,
Damian
Okay, given that we're dealing with column vectors of different size, you can't put them all in a numerical array, since a numerical array has to be rectangular. If you really wanted to put them in the numerical array, then the column length of the array will need to be the length of the longest vector, and you'll have to pad out the shorter vectors with NaNs.
Given this, a better solution would be, as chaohuang hinted at in the comments, to use a cell array, and store one vector in each cell. The problem is that you don't know beforehand how many vectors there will be. The usual approach that I'm aware of for this problem is as follows (but if someone has a better idea, I'm keen to learn!):
UpperBound = SomeLargeNumber;
Array = cell(1, UpperBound);
Counter = 0;
while SomeCondition
Counter = Counter + 1;
if Counter > UpperBound
error('You did not choose a large enough upper bound!');
end
%#Create your vector here
Array{1, Counter} = YourVectorHere;
end
Array = Array(1, 1:Counter);
In other words, choose some upper bound beforehand that you are sure you won't go above in the loop, and then cut your cell array down to size once the loop is finished. Also, I've put in an error trap in case you're choice of upper bound turns out to be too small!
Oh, by the way, I just noted in your question the words "sum up several vectors". Was this a figure of speech or did you actually want to perform a sum operation somewhere?

Can someone please clarify the Birthday Effect for me?

Please help interpret the Birthday effect as described in Wikipedia:
A birthday attack works as follows:
Pick any message m and compute h(m).
Update list L. Check if h(m) is in the list L.
if (h(m),m) is already in L, a colliding message pair has been found.
else save the pair (h(m),m) in the
list L and go back to step 1.
From the birthday paradox we know that we can expect to find a
matching entry, after performing about
2^(n/2) hash evaluations.
Does the above mean 2^(n/2) iterations through the above entire loop (i.e. 2^(n/2) returns to step 1), OR does it mean 2^(n/2) comparisons to individual items already in L?
It means 2^(n/2) iterations through the loop. But note that L would not be a normal list here, but a hash table mapping h(m) to m. So each iteration would only need a constant number (O(1)) of comparisons in average, and there would be O(2^(n/2)) comparisons in total.
If L had been a normal array or a linked list, then the number of comparisons would be much larger since you would need to search through the whole list each iteration. This would be a bad way to implement this algorithm though.