I want to change a single element in a vector:
function vecteur_avec_delai=delai(input_vecteur, iteration)
vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
end
input_vecteur is a vector of size 6001 -by- 1 filled with the same value: 61.46 , and vecteur_avec_delai = ones(6001,1)
I want to change one value of vecteur_avec_delai to 61.46, so to still have a vector of length 6001, filled with ones except one cell with a value equal to 61.46, thus the vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
When I run :
vecteur_avec_delai=delai(input_vecteur, iteration)
It compiles but gives me for output a vecteur_avec_delai of size (iteration, 1 ) filled with 0s except for the last value equal to 61.46.
When I try this operation "manually" (directly in the MATLAB command window), it works, so why doesn't it when I go through this function?
You haven't declared vecteur_avec_delai as input into the function. This boils down to calling A(10,1) = 1 in a clean command window: it creates a 10-by-1 vector with 9 zeros and a 1 at the end. Instead, first declare your array to be ones and then set only this required element:
function vecteur_avec_delai=delai(input_vecteur, iteration)
vecteur_avec_delai = ones(size(input_vecteur));
vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
end
Or, given your comment about it working in the command window, simply add vecteur_avec_delai to the inputs of the function (you might want to change the names of the input and output variables to not be the same for clarity). Functions are closed name-spaces, i.e. they have their own "workspace" so to say, and cannot see anything outside of that function, hence the need to declare all inputs.
Related
I'm quite new to programming and GDScript and wondering how to do something that I did think would be quite simple, but I'm struggling a bit!
I've loaded an array of images and I want it to go through each of these images one after the other each time a button is clicked and replace a sprite texture with that particular image.
Right now I can successfully get the sprite to change to any of the images if I put its array number in e.g. new_texture[0] or new_texture[3] etc., but I would like it to go through each of the images one after the other every time the user clicks the button. (And once it's got to the last image, go back to the first image again)
What am I missing?
Here is the code:
extends Node
onready var my_sprite = get_node("/root/Game/Picture/Sprite")
var new_texture = [
load("res://Art/Picture1.png"),
load("res://Art/Picture2.png"),
load("res://Art/Picture3.png"),
load("res://Art/Picture4.png"),
load("res://Art/Picture5.png"),
load("res://Art/Picture6.png")
]
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[0]
As you know, you can get the first image of the array like this:
new_texture[0]
Where new_texture is a variable that holds the array, and 0 is a constant (a literal, to be more precise) that indicates the position on the array.
So you can change the constant to get a different image, for example the fourth element is:
new_texture[3]
Now, that is fine if you want to change which element you get form one version to another of the game, since it requires to modify the codeā¦
But you want which element from the array you get to change during the execution of the program. In other words, you don't want it to be constant, but variable.
So declare a variable that will hold which element you get:
var texture_index := 0
Use it instead of the constant:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
And now you can change which element from the array you get by changing the value of the variable, for example:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
texture_index += 1
Here we are changing the value of texture_index so the next time this code executes it gets the next position.
But be careful to keep the value of the variable inside the bounds of the array to avoid errors. So, what do you want to do after it reaches the last element? Do you want it to loop? Well, we can check if we reached the last element with an if, and set a different value to the variable, like this:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
texture_index += 1
if texture_index == new_texture.size():
texture_index = 0
I remind you that elements of the array go from the position 0 to the position array.size() - 1. For example, if the array has 10 elements, they are in the positions 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (from 1 to 9, there are 9 numbers, plus the 0 you have 10 numbers). So an array with 10 elements does not have an element in the position 10. So there is no element in the position array.size(), thus, when the index reaches that value it is out of bounds of the array, and we reset it to the first element (position 0).
There are, of course, variation of this approach.
I want to use a random number from a list to select the particular element of a parameter array and use it elsewhere in the script as a parameter e.g.
Array is sspaidlist
My random integer from a parameter list is {GenRandomSSPAID}, which i want to use as the element of the sspaidlist array and save to is RandomSSPAID
lr_save_string(lr_eval_string("sspaidlist_{GenRandomSSPAID}"),"RandomSSPAID");
This just gets me the actual value "sspaidlist" and not the array.
I've also tried
sprintf(RandomSSPAID, "{sspaidlist_%d}", lr_eval_string("{GenRandomSSPAID}"));
but this seems to set RandomSSPAID to 0
The idea is to get 3 unique values - so 3 different array elements, I can't get the same value twice. I've offloaded the randomness to the loadrunner parameter functions, so I will always get a unique number with {GenRandomSSPAID}.
First convert your "GenRandomSSPAID" to integer as below:
i = atoi(lr_eval_string("{GenRandomSSPAID}"));
Now use sprintf to save it into RandomSSPAID as below:
sprintf(RandomSSPAID, "{sspaidlist_%d}", i);
You should be able to see value now.
I resolved this with the following code:
//declare c variables
Add_List()
{
....
char *RandomSSPAID;
char *SecondRandomSSPAID;
char *ThirdRandomSSPAID;
RandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(RandomSSPAID),"RandomSSPAID");
SecondRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(SecondRandomSSPAID),"SecondRandomSSPAID");
ThirdRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(ThirdRandomSSPAID),"ThirdRandomSSPAID");
lr_error_message("Random Values for iteration %s are : %s_%s_%s",lr_eval_string("{IterationNumber}"),lr_eval_string("{RandomSSPAID}"),lr_eval_string("{SecondRandomSSPAID}"),lr_eval_string("{ThirdRandomSSPAID}"));
....
}
Note that I offloaded the randomness to Loadrunner to generate a random number with {GenRandomSSPAID}, which is a parameter type of File, with a list of numbers and setting to select new row 'Random', Update value on 'Each occurance'
I am trying to pull out a column from inside a cell. However, sometimes, the cell is empty.
For example, if in this line, I try to pull out the last column inside PM25_win{i}, it sometimes has an array inside of size nx28. However, sometimes, the array is zero.
for i = 1:length(years)-1
PM25 = table2array(PM25_win{i}(:,end));
end
When the array is empty, the code stops and I get the error
Subscript indices must either be real positive integers or logicals.
How can I account for both cases so that the code will simply create the PM25 variable as an empty array if PM25_win{i} is empty?
You could simply add an if-else statement in the for loop.
for i = 1:length(years)-1
if isempty(PM25_win{i}(:,end))
PM25 = [];
else
PM25 = table2array(PM25_win{i}(:,end));
end
end
I want to slice an array to create a new array containing all but the first entry in the array.
This is what i tried:
#arguments = $splittedCommands[1..-1];
Which gives me an empty result.
1 should be the second entry and -1 should be the last, so why doesn't this work?
You should use # in front of array when slicing an array ($ tells that your're accessing single scalar value inside array), so
my #arguments = #splittedCommands[ 1 .. $#splittedCommands ];
Second, your range should be either -$#splittedCommands .. -1 or 1 .. $#splittedCommands with later being more common and straightforward.
Easiest may be to assign it into another list, using
( undef, my #commands ) = #splittedCommands;
Assigning into undef here throws away the first result, then the remainder goes into #commands
Another approach could be to assign the lot and then remove the first
my #commands = #splittedCommands;
shift #commands;
This could also be simplified if you didn't need to keep the original array around any more; just shift the first item off #splittedCommands then use it directly.
If the original array must be left intact then the obvious way seems to be to copy the whole array and remove the first element.
shift(my #commands = #splittedCommands)
will do just fine
I've got a nested for loop, in the inner loop I've got an array that will change size and value in each iteration,e.g;
a=[ 2 3 4]
and in the next iteration it will be :
a=[9 5]
but the result of my code is :
a=[9 5 4]
a(3) is the problem, it is from the previous iteration and I don't want it,so what should I do?
I do not know how to write my code here cause it contains lots of functions and you wont understand it!?
but it's sth like this:
for j=1: 5
%l is the length of row in cell array(a) that varies from one row to another
for i=1:l
dn=a{j,i};
spp(t)=dn(1)
end
targ{j,1}=spp;
end
spp is the problem here
Insert a clear command to delete the temporary variable (once spp have three elements, it never goes back to a 2 elements vector unless you clear it or declare it).
...
targ{j,1}=spp;
clear spp;
...
Alternatively, you can code the matlab-way by declaring your variable before it gets populated. In this situation, there is no need for a clear command.
for j=1:5
%l is the length of row in cell array(a) that varies from one row to another
spp = zeros(1,l);
for i=1:l
...