iterating over multiple intervals Matlab - matlab

I'd like to make a loop in Matlab that would work only over user specified intervals of time, instead of simply the whole time. How can one write this loop condition?
regards

Generally you and iterate multiple ways. There are two main ways that I can think of off the top of my head. I will also open this up for community wiki so others can just easily edit this as well.
First, using a simple For loop.
The general syntax is for index = 1:someValue
someValue can really be anything that is greater than 1 in this case. Many times it's simply the last index value of a matrix you're wanting to traverse.
You can modify this as well though! Let's say you want every 3rd index starting with the 2nd position in the matrix. All you have to do is: for index = 2:3:someValue The 3 here tells the loop that it should add 3 to the index at the end of each loop iteration until you get to (or surpass) someValue.
Yet another modfication is traversing backwards. In this case you start at the 'end' and move to the beginning. It would look like this: for index = someValue:-1:1 You can also do every 4th index while going backwards for index = someValue:-4:1.
Obviously you can replace the value between the two : to be a step size that you want. You just have to be aware of potential indexing issues that can arrise. Namely trying to index out of bounds of the matrix or potentially going negative.
Secondly here you can modify the value within the loop itself. Generally this isn't the best idea when you're using a for loop as the value of your indexing variable will be changed by the loop as well as by you within the loop. Many times you'll see this done with a while loop or a do while combo.
A few examples are as follows:
index = 0;
do
{
% some operations
index = index + aNumber;
}while(index <= someValue)
In the above example the loop will continue to loop until the index value index becomes greater than or equal to someValue at the end of the current iteration of the loop. This type of loop will ALWAYS, and I repeat ALWAYS execute at least once.
index = 0;
while(index <= someValue)
{
%some operations
index = index + aNumber;
}
In this case the loop will continue to loop while index satisfies the logical statement here. If the statement isn't true when you try to run the loop for the first time it will not execute at all.
Hope this helps and feel free to ask for any additional clarification if you want it!
Others, please feel free to edit to add additional information or clean up what I might have not explained fully =)

Do you mean something like this:
for i = [1:5 7:10 12:14 21:22]
do_func(i)
end
or even
for i = [1:5 4:7 19:-1:15]
do_func(i)
end
?

If you only want to perform an action when a certain condition is true in a loop, you just need something like:
for i=1:length(arr)
if condition(arr(i))
do_something(arr(i));
end
end

Related

matlab: check which lines of a path are used - graphshortestpath

The related problem comes from the power Grid in Germany. I have a network of substations, which are connected according to the Lines. The shortest way from point A to B was calculated using the graphshortestpath function. The result is a path with the used substation ID's. I am interested in the Line ID's though, so I have written a sequential code to figure out the used Line_ID's for each path.
This algorithm uses two for loops. The first for-loop to access the path from a cell array, the second for-loop looks at each connection and searches the Line_ID from the array.
Question: Is there a better way of coding this? I am looking for the Line_ID's, graphshortestpath only returns the node ID's.
Here is the main code:
for i = i_entries
path_i = LKzuLK_path{i_entries};
if length(path_i) > 3 %If length <=3 no lines are used.
id_vb = 2:length(path_i) - 2;
for id = id_vb
node_start = path_i(id);
node_end = path_i(id+1);
idx_line = find_line_idx(newlinks_vertices, node_start, ...
node_end);
Zuordnung_LKzuLK_pathLines(ind2sub(size_path,i),idx_line) = true;
end
end
end
Note: The first and last enrty of path_i are area ID's, so they are not looked upon for the search for the Line_ID's
function idx_line = find_line_idx(newlinks_vertices, v_id_1, v_id_2)
% newlinks_vertices includes the Line_ID, and then the two connecting substations
% Mirror v_id's in newlinks_vertices:
check_links = [newlinks_vertices; newlinks_vertices(:,1), newlinks_vertices(:,3), newlinks_vertices(:,2)];
tmp_dist1 = find(check_links(:,2) == v_id_1);
tmp_dist2 = find(check_links(tmp_dist1,3) == v_id_2,1);
tmp_dist3 = tmp_dist1(tmp_dist2);
idx_line = check_links(tmp_dist3,1);
end
Note: I have already tried to shorten the first find-search routine, by indexing the links list. This step will return a short list with only relevant entries of the links looked upon. That way the algorithm is reduced of the first and most time consuming find function. The result wasn't much better, the calculation time was still at approximately 7 hours for 401*401 connections, so too long to implement.
I would look into Dijkstra's algorithm to get a faster implementation. This is what Matlab's graphshortestpath uses by default. The linked wiki page probably explains it better than I ever could and even lays it out in pseudocode!

I need a function that will return the existence (or not) of a given index of a matrix

I'm trying to filter noise out of an image in matlab and I've hit an issue. I need to be able ask some function whether or not if the index -1,-3 or -1,4 or 5,-1 exists, and I need it to return some integer or a false so that I can put it in an if statement. so far, with islogical() exist() and just ARRAYNAME(-1,4) I've gotten an error saying that index position doesn't exist (duh, but that's not what I want) is there a function that can return 1 if there's an error? I really just need this one thing. let me know if the question is too vague.
You can use try-catch statement as follows.
function element=neverReturnIndexingError(array1)
%array1=[1 2 3 4];
try
element=array1(-1,2);
catch
fprintf('Index is invalid\n');
element=1; %returning 1 as you said
end

Debugging a for loop in matlab

I've been looking throught the documentation, but can't seem to find the bit I want.
I have a for loop and I would like to be able to view every value in the for loop.
for example here is a part of my code:
for d = 1 : nb
%for loop performs blade by blade averaging and produces a column vector
for cc = navg : length(atbmat);
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
atbvec2(:,cc) = atb2;
end
%assigns column vector 'atbvec2' to the correct column of the matrix 'atbmat2'
atbmat2(d,1:length(atbvec2)) = atbvec2;
end
I would like to view every value of atb2. I'm a python user(new to MATLAB) and would normally use a simple print statement to find this.
I'm sure there is a way to do it, but I can't quite find how.
Thankyou in advance.
you can use disp in Matlab to print to the screen but you might want to use sprintf first to format it nicely. However for debugging you're better off using a break point and then inspect the variable in the workspace browser graphically. To me, this is one of Matlab's best features.
Have a look at the "Examine Values" section of this article
The simplest way to view it everywhere is to change this line:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
Into this, without semicolon:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg
That being said, given the nature of your calculation, you could get the information you need as well by simply storing every value of abt2 and observing them afterwards. This may be done in atbmat2 already?
If you want to look at each value at the time it happens, consider setting a breakpoint or conditional breakpoint after the line where abt2 is assigned.

How to rename a variable in a loop in MATLAB?

Can somebody please tell if there exists a way to rename a variable in each iteration of a loop in MATLAB?
Actually, I want to save a variable in a loop with a different name incorporating the index of the loop. Thanks.
Based on your comment, I suggest using a cell array. This allows any type of result to be stored by index. For example:
foo=cell(bar,1);
for ii=1:bar
foo{ii}=quux;
end
You can then save foo to retain all your intermediate results. Though the loop index is not baked into the variable name as you want, this offers identical functionality.
Ignoring the question, "why do you need this?", you can use the eval() function:
Example:
for i = 1:3
eval(['val' num2str(i) '=' num2str(i * 10)]);
end
The output is:
val1 =
10
val2 =
20
val3 =
30
Another way, using a struct to save the loop index into the name of the field:
for ii=1:bar
foo.(["var" num2str(ii)]) = quux;
end
This creates a structure with fields like foo.var1, foo.var1 etc. This does what you want without using eval.

Get all top level entry values from NotesView

What is the easiest way to get all top level entry values from a notes view? I have found the property "TopLevelEntryCount" returning the number of alle categories, but I want the values, too.
To iterate the view entries and column values need too much time.
Set ecl = view.Allentries
Set ve = ecl.Getfirstentry()
While Not(ve Is Nothing)
If IsArray(ve.Columnvalues(0)) Then
If flag = "" Then
arr = ve.Columnvalues(0)
Else
arr = ArrayUnique(ArrayAppend(arr, ve.Columnvalues(0)))
End If
Else
'error if arr is not already an array
arr = ArrayUnique(ArrayAppend(arr, ve.Columnvalues(0)))
End If
flag = "1"
Set ve = ecl.Getnextentry(ve)
Wend
Anyone know a faster way? It must not be written in LotusScript, actually I would prefer Java, JS or SSJS.
Idea 1: You can use the NotesViewNavigator class, and call the getFirst() and getNextCategory() methods. This should be faster than walking all the entries.
Idea 2: You can use NotesSession.Evaluate() with a formula that does an #Unique on #DbColumn for the first column of view. That should bring you back an array, and you can get the ubound of the array. Formulas tend to be very fast, but evaluate() has to compile them first, so I don't know if this will be faster or not. The disadvantage of this approach is that for very large views, this could exceed formula language's size limits. But if it does prove to be faster, you could catch the exception and fall-back to the slower method of iterating.
Richard's response is perfect!
For a java version it's almost the same:
see the Domino help for getNextCategory the given example below ViewNavigator really answer your need: link here
The evaluate method is also available in java Session