how to access string declared variables in scilab - scilab

I am having trouble with is line "P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2);"
basically I have declared matrix P_dot_ij_om_1=[] from 1 to i
and in the next line.
I would like to input data e.g. P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2);
where ii is a number. what is the right expression.
rows=round(k/360);
i=1;
ii=1;
k=1;
while ii <= rows
Pdot_names(ii,1)=string("P_dot_ij_om_"+ string(ii));
disp(Pdot_names(ii))
execstr(Pdot_names(ii)+'=[]'); // declare indexed matrix
while i<361
P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2);
// P_dot_ij_om_+ii(i,2)=P_dot_ij(k,3);
disp(k)
k=k+1;
i=i+1;
end
ii=ii+1;
end

The code below works, but in general it is not advised to create string variables. There are much faster and also easier to implement alternatives, see e.g. this thread: How can I create variables Ai in loop in scilab, where i=1..10
rows = 2;
P_dot_ij = [10,20,30;11,21,31;12,22,32];
i=1;
ii=1;
k=1;
while ii <= rows
//Pdot_names(ii,1)=string("P_dot_ij_om_"+ string(ii)); The firs 'string' is unnecessary
Pdot_names(ii,1)="P_dot_ij_om_"+string(ii);
disp(Pdot_names(ii))
execstr(Pdot_names(ii)+'=[]'); // declare indexed matrix
while i<4
//P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2);
execstr("P_dot_ij_om_"+string(ii)+"("+string(i)+",1)=P_dot_ij("+string(k)+",2)"); // P_dot_ij_om_+ii(i,2)=P_dot_ij(k,3);
execstr("P_dot_ij_om_"+string(ii)+"("+string(i)+",2)=P_dot_ij("+string(k)+",3)");
disp(k)
k=k+1;
i=i+1;
end
ii=ii+1;
end
disp(P_dot_ij_om_1,"P_dot_ij_om_1");
disp(P_dot_ij_om_1,"P_dot_ij_om_2");
And also next time please post selfcontained code examples, because otherwise I can only guess what is k and P_dot_ij.

Related

Need to change an element of a multidimensional string vector in C++

I have a multidimensional string vector, gameState. The contents of gameState can be seen below. I would like to "move" A up one space by making [2][1] = " " and [1][1] = "A". But I'm getting an error (error: invalid conversion from 'const char*' to '__gnu_cxx::__alloc_traitsstd::allocator<char, char>::value_type' {aka 'char'} [-fpermissive]). Not sure if there is a way to use vector.at() in this case. Here is a snippet. The full code reads in the initial game state from a text file and assigns it to the string vector gameState. I get the row and col indexes for each letter in the grid below and assign them to variables (eg. Arow and Acol). I'm very new to C++, so any help would be appreciated. If you have any questions about the rest of the code, I'd be happy to elaborate. Thanks in advance.
int Arow;
int Acol;
vector<string> gameState;
string tempU = gameState[Arow-1][Acol];
if (tempU != "#") {
// if no wall is up, move up
gameState[Arow][Acol] = " ";
gameState[Arow-1][Acol] = "A";
Arow = Arow-1;
Amovesequence.push_back("U");
}
###########
#...#P...B#
#A#.$.###*#
#...#D...R#
###########
The type of gameState[Arow-1][Acol] is a char, not a string. Modify the code accordingly, e.g.: char tempU = ... and replace " " with ' 'etc.

2d array gamemaker2 studio

Experienced programmer playing around with Gamemaker2 Studio.
Trying to draw some random squares on the screen using a 2D array to store the "map"
Step 1 : declare a 2D array MyMap[25,25] this works
Step 2 : Set 100 random locations in Map[]=1 this works
I get a crash when I try to look up the values I have stored in the array.
Its crashing with:
**Execution Error - Variable Index [3,14] out of range [26,14] **
So it looks like it is trying to read 26 element, when you can see from my code the for next loop only goes to 20 and the array bound is 25.
Oddly enough it does the first two loops just fine?
Looking like a bug, I've spent so much time trying to work it out, anyone got an idea what is going on?
var tx=0;
var ty=0;
var t=0;
MyMap[25,25]=99; **// Works**
for( t=1; t<100; t+=1 ) **// Works**
{
MyMap[random(20),random(15)]=1
}
for( tx=1; tx<20; tx+=1 )
{
for( ty=1; ty<15; ty+=1 )
{
show_debug_message(string(tx) + ":" + string(ty))
t = MyMap[tx,ty]; /// **<---- Crashes Here**
if t=1 then {draw_rectangle(tx*32,ty*32,tx*32+32,ty*32+32,false) }
}
}
The line MyMap[random(20),random(15)]=1 does not initialize values in the entire array, creating a sparse array(where some elements do not exist).
The line MyMap[25,25]=99;
Should read:
for( tx=1; tx<20; tx+=1 )
{
for( ty=1; ty<15; ty+=1 )
{
MyMap[tx,ty]=99;
}
}
This will pre-initialize the all of the array values to 99. Filling out the array.
Then you can randomly assign the ones. (You will probably get less than 100 ones the due to duplicates in the random function and the random returning zeros.)
You should have the above code in the Create Event, or in another single fire or controlled fire event, and move the loops for the draw into the Draw Event.
All draw calls should be in the Draw Event. If the entire block were in Draw, it would randomize the blocks each step.

R: Iterating Over the List

I am trying to implement following algorithm in R:
Iterate(Cell: top)
While (top != null)
Print top.Value
top = top.Next
End While
End Iterate
Basically, given a list, the algorithm should break as soon as it hits 'null' even when the list is not over.
myls<-list('africa','america south','asia','antarctica','australasia',NULL,'europe','america north')
I had to add a for loop for using is.null() function, but following code is disaster and I need your help to fix it.
Cell <- function(top) {
#This algorithm examines every cell in the linked list, so if the list contains N cells,
#it has run time O(N).
for (i in 1:length(top)){
while(is.null(top[[i]]) !=TRUE){
print(top)
top = next(top)
}
}
}
You may run this function using:
Cell(myls)
You were close but there is no need to use for(...) in this
construction.
Cell <- function(top){
i = 1
while(i <= length(top) && !is.null(top[[i]])){
print(top[[i]])
i = i + 1
}
}
As you see I've added one extra condition to the while loop: i <= length(top) this is to make sure you don't go beyond the length of the
list in case there no null items.
However you can use a for loop with this construction:
Cell <- function(top){
for(i in 1:length(top)){
if(is.null(top[[i]])) break
print(top[[i]])
}
}
Alternatively you can use this code without a for/while construction:
myls[1:(which(sapply(myls, is.null))[1]-1)]
Check this out: It runs one by one for all the values in myls and prints them but If it encounters NULL value it breaks.
for (val in myls) {
if (is.null(val)){
break
}
print(val)
}
Let me know in case of any query.

systemverilog constraint dist using weights array

I need to be able to set a constraint dist with 64 different, changeble weights:
I need to random pick an index of range 0~63, when every index has its own weight / probability to be chosen.
I can write something like:
constraint pick_chan_constraint {pick_channel dist{
0:=channel_weight[0], 1:=channel_weight[1], 2:=channel_weight[2],
3:=channel_weight[3], 4:=channel_weight[4], 5:=channel_weight[5],
6:=channel_weight[6], 7:=channel_weight[7], 8:=channel_weight[8],
9:=channel_weight[9], 10:=channel_weight[10], 11:=channel_weight[11],
12:=channel_weight[12], 13:=channel_weight[13],
14:=channel_weight[14], … ...
NUM_OF_CHANS-1 := channel_weight[NUM_OF_CHANS-1] }}
Obviously it's bad writing and a bad idea, out of 2 reasons:
No flexibility- if NUM_OF_CHANS changes, I'll need to change the code.
It's long and ugly and almost unreadable.
Any ideas?
Thanks
IEEE Std 1800-2012 § 18.5.4 Distribution shows the dist_list needs to be a list of dist_items and a dist_item is defined as a value_range [ dist_weight ]. In other words the distribution needs to be listed out.
Instead of using a constraint you could create a queue array (§ 7.10 Queues) and then use the shuffle method (§ 7.12.2 Array ordering methods). Example:
int channel_weight [64];
int pick_channel;
int weight_chain [$];
weight_chain.delete(); // make sure it is empty
foreach (channel_weight[i]) begin
repeat (channel_weight[i]) begin
weight_chain.push_back(i);
end
end
weight_chain.shuffle(); // randomize order
assert( weight_chain.size() > 0) else $error("all channel_weights are 0");
pick_channel = weight_chain[0];

SQLite seek question

I'm using SQLite version 3 to run the following code.
//Run SQL SELCT query
sqlite3_prepare16_v2("SELECT * FROM table_name");
sqlite3_step();
//Need to review results in several iterations
for(int i = 0; i < n; i++)
{
//Seek to beginning
sqlite3_reset();
do
{
//Get values
sqlite3_column_int();
...
sqlite3_column_text16();
}
while(sqlite3_step() == SQLITE_ROW);
}
But for some reason the first batch of data I get is all 0's. What am I not doing correct in the code above?
Assuming what you are showing is pseudocode since you're missing many arguments to the sqlite3 funcitons...
You need a sqlite3_step after sqlite3_reset and before getting the first row of values.
You can change your do {...} while(sqlite3_step() == SQLITE_ROW) to a while(sqlite3_step() == SQLITE_ROW)...} to accomplish that.
This will also eliminate the need to step immediately after the sqlite3_prepare16_v2

Resources