I created a structure containing 7 variables by using the multpots function in the BRML toolkit. The structure represents a joint probability distribution function. I would like to remove the rows that has 0 probability. How do I do this in matlab?
If you are using a struct to hold data and each field corresponds to a variable, you can delete the row having 0 probability in this way:
testData([testdata.probability] = 0) = [];
Where testdata variable holds the struct of data. Another way to do so is:
newTestData= subsetstruct(testData,testData.probability = 0);
Related
I am implementing the merge sort algorithm in Python. Previously, I have implemented the same algorithm in C, it works fine there, but when I implement in Python, it outputs an unsorted array.
I've already rechecked the algorithm and code, but to my knowledge the code seems to be correct.
I think the issue is related to the scope of variables in Python, but I don't have any clue for how to solve it.
from random import shuffle
# Function to merge the arrays
def merge(a,beg,mid,end):
i = beg
j = mid+1
temp = []
while(i<=mid and j<=end):
if(a[i]<a[j]):
temp.append(a[i])
i += 1
else:
temp.append(a[j])
j += 1
if(i>mid):
while(j<=end):
temp.append(a[j])
j += 1
elif(j>end):
while(i<=mid):
temp.append(a[i])
i += 1
return temp
# Function to divide the arrays recursively
def merge_sort(a,beg,end):
if(beg<end):
mid = int((beg+end)/2)
merge_sort(a,beg,mid)
merge_sort(a,mid+1,end)
a = merge(a,beg,mid,end)
return a
a = [i for i in range(10)]
shuffle(a)
n = len(a)
a = merge_sort(a, 0, n-1)
print(a)
To make it work you need to change merge_sort declaration slightly:
def merge_sort(a,beg,end):
if(beg<end):
mid = int((beg+end)/2)
merge_sort(a,beg,mid)
merge_sort(a,mid+1,end)
a[beg:end+1] = merge(a,beg,mid,end) # < this line changed
return a
Why:
temp is constructed to be no longer than end-beg+1, but a is the initial full array, if you managed to replace all of it, it'd get borked quick. Therefore we take a "slice" of a and replace values in that slice.
Why not:
Your a luckily was not getting replaced, because of Python's inner workings, that is a bit tricky to explain but I'll try.
Every variable in Python is a reference. a is a reference to a list of variables a[i], which are in turn references to a constantant in memory.
When you pass a to a function it makes a new local variable a that points to the same list of variables. That means when you reassign it as a=*** it only changes where a points. You can only pass changes outside either via "slices" or via return statement
Why "slices" work:
Slices are tricky. As I said a points to an array of other variables (basically a[i]), that in turn are references to a constant data in memory, and when you reassign a slice it goes trough the slice element by element and changes where those individual variables are pointing, but as a inside and outside are still pointing to same old elements the changes go through.
Hope it makes sense.
You don't use the results of the recursive merges, so you essentially report the result of the merge of the two unsorted halves.
How can I assign each document in a collection C a unique value from an array?
Let's say I create the array let A = 1..count(C). How can I give each document in C a number from A, making sure each element of A is used only once?
There's are unfortunately no Python-like iterators enumerate() and zip() in AQL, which would make this really easy and efficient. It is still possible however, but can be a costly operation (mainly memory-intense) for very large datasets, because all document keys need to be fetched and stored in memory:
LET arr = RANGE(1, LENGTH(C) * 2, 2)
LET docs = (FOR doc IN C RETURN doc._key)
FOR n IN 0..LENGTH(docs)-1
UPDATE docs[n] WITH {number: arr[n]} IN test
RETURN NEW
An array arr is generated (here: as many odd numbers as there are documents in the collection C), and a variable docs holds all document keys of C. A loop over the range 0..n with n = number of documents in C is used for a counter n. Then, the nth document key of docs is used to update a document with the nth number from the array arr.
If you want to assign A = 1..LENGTH(C), then a slightly simpler query can be used:
LET docs = (FOR doc IN C RETURN doc._key)
FOR n IN 1..LENGTH(docs)
UPDATE docs[n-1] WITH {number: n} IN test
RETURN NEW
This question already has answers here:
Create variables with names from strings
(6 answers)
Closed 6 years ago.
I have a loop running on multiple workstations, each loop is dealing with 5 different classes, when the results is acquired it is to save the result with the name of the classes it used.
For example on workstation 1 I am using:
class1 = 1;
class2 = 10;
%code that will run on all images associated with classes 1:10
% final_result from all 10 classes is calculated here
I want to save this now, with the name such as:
result_1_10 = final_result;
save result_1_10 result_1_10;
I can do this manually but its becoming very difficult to change the values on all machines after one value is changed, I would rather it save these and pick up the numbers from the two variables class1 and class2.
Here is what I tried:
['result_' num2str(class1) '_' num2str(class2)];
This would give me result_1_10. Which is what I wanted but it is a string, rather than a variable, so I cannot assign the result value to it
['result_' num2str(class1) '_' num2str(class2)] = final_result;
Would give the error:
Error: An array for multiple LHS assignment cannot contain
LEX_TS_STRING.
I even tried str2num(num2str(class1)) but that would also give an error.
How do I actually do this?
Thank you
While you can do this, it is very much discouraged by The Mathworks themselves. Any time you are trying to store information about what a variable contains within the variable name itself, that's a sign that maybe things should be rearranged a bit. Maybe consider using a different data structure.
Consider for example using a struct where you keep the classes as fields and the result as a field.
S.class1 = 1;
S.class2 = 10;
S.result = final_result;
You could then even create an array of structs holding your data.
S = struct('class1', {1, 2, 1}, ...
'class2', {10, 11, 10}, ...
'result', {rand(10), rand(10), rand(10)});
Then you could grab all results when class1 was 1:
S([S.class1 == 1]);
Or all results when class1 as 1 and class2 was 10
S([S.class1 == 1] & [S.class2 == 10]);
If you insist on doing it the way that you've laid out, you'll have to use eval or assignin to do that. Also, sprintf is often more concise than string concatenations.
variable = sprintf('result_%d_%d', class1, class2);
eval([variable, '= final_result;']);
I have a follow up question on this post. I would like to take the contents of an Excel spreadsheet and put it into a Array of tuples where each tuple corresponds to each row in the spreadsheet.
I started with looping though the entire range like this:
let path = "XXX.xlsx"
let app = ApplicationClass(Visible = false)
let book = app.Workbooks.Open path
let sheet = book.Worksheets.[1] :?> _Worksheet
let content = sheet.UsedRange.Value2 :?> obj[,]
for i=content.GetLowerBound(0) to content.GetUpperBound(0) do
for j=content.GetLowerBound(1) to content.GetUpperBound(1) do
But strikes me as very inefficient. If there something in the base API spec out of the box that I can use?
Thanks in advance
The Array2D module implements some common functions for 2D arrays.
I am guessing you want to use Array2D.iter or Array2D.iteri to replace your for loop.
The straightforward way to convert each row (of a two-dimensional array) to a tuple (in a single-dimensional row) is to finish what you started - just iterate over all the rows and construct a tuple:
let tuples =
[ for i in contents.GetLowerBound(0) .. contents.GetUpperBound(0) ->
contents.[i,0], contents.[i,1], contents.[i,2] ]
To do this, you need to know (statically) what is the length of the row. This is because F# tuples are fixed-length tuples and the length is checked. The above example assumes that there are just 3 elements with indices 0, 1 and 2. If the length is dynamic, then you probably should continue using 2D arrays rather than a list of tuples.
Another option is to bypass the Excel engine altogether. There are a couple of ways of doing this, but the one I've had great success with is 'SpreadsheetGear'. It isn't free (bring on the downvotes), but it replicates the Excel API very closely and is very fast.
I have a script something like the following:
for
(do something)
end
And also outputs that use data output from the loop (which change each time -- when the script is run):
A = 1
A = 1.5
Etc.
I am looking to store this output that changes each time into a matrix.
Is this feasible?
for number of iterations
(Call script)
end
Output to excel
The reason I want to store the data into a matrix is to be able to output all the answers (for several iterations) into Excel at once.
Edit:
To give a better picture of what my output looks like it is something like this
Output = [rand() rand() rand(); rand() rand() rand()];
then I use this to create a new variable:
var = Output(1,1)./Output(2,1);
each time I run the script the answer changes. This new answer each time, is what I am looking to save in a matrix. Hope that clears things up.
Depending on the type of output/outputs from each loop you can, trivially, save intermediate results in one out of many MATLAB data structures(randn is used in the following as a sample of "do something"):
nIterations = 10;
% scalar output
A = zeros(1, nIterations);
for n=1:nIterations
A(n) = randn;
end
% matrix ouput of possibly changing size
B = cell(1, nIterations);
for n=1:nIterations
B{n} = randn(1, n+1);
end
% matrix output of fixed size
C = zeros(3, 3, nIterations);
for n=1:nIterations
C(:,:,n) = randn(3, 3);
end
Assuming that var is the thing you want to put in the matrix after each iteration, I suggest the following:
Add another for loop around your code, for example loop over i, then in the end do not assign the value to var, but to var(i).
Depending on your output you need to choose the variable type of var, for example cell or matrix.