I have an array of nested structure. for example
st(1).a.b.c=1
st(2).a.b.c=2
st(3).a.b.c=3
...and so on
If I wanted to find the index number of the '.c' objects containing the number 3, I try the following function
find([st.a.b.c]==3)
It gives this error:
Expected one output from a curly brace or dot indexing expression, but there were 3 results.
Could anybody help me to solve this problem?
As you may have figured it out, handling multi-level indexing in structures is a bit confusing. However, may find this helpful:
st(1).a.b.c=1;
st(2).a.b.c=2;
st(3).a.b.c=3;
checkLoop = 1;
while checkLoop
if isstruct(st)
fieldNm = fieldnames(st); % In case you have single field in each level
st = [st(:).(fieldNm{1})];
else
checkLoop = 0;
end
end
where3 = find(st == 3);
Related
I am new to the game maker. I created a list and I want to compare all the data in the list with a specific value. I used the following code:
for(var i=0;i<ds_list_size(lst);i++;)
{
if ds_list_find_value(lst,i)>tmp
ds_list_replace(lst,i,ds_list_find_value(lst,i)-1);
}
and I face the following error:
Push :: Execution Error - Variable Get -1.lst(100001, -1)
at
gml_Object_object0_RightButtonPressed_1 (line 21) - for(var i=0;i
where is my problem?
Thanks all.
if your first for loop i = 0; and when the first entry in the list is smaller than tmp it tries to replace the first place in the list with a not existing one. so you could either check if its the first entry of the list with
if ( i == 0 ) { }
or your could start the for loop from the second entry with
for(var i=1;i<ds_list_size(lst);i++;)
I think the ; at the end of i++; is unnecessary, you only need to use ; in a for-loop as seperator.
GML gives more freedom to common C# rules though (like how there are no brackets needed around an if-condition), so perhaps that's allowed.
Another possibility might be that the index is out of range at ds_list_replace()
I try to write a simple console application in C++ which can read any chemical formula and afterwards compute its molar mass, for example:
Na2CO3, or something like:
La0.6Sr0.4CoO3, or with brackets:
Fe(NO3)3
The problem is that I don't know in detail how I can deal with the input stream. I think that reading the input and storing it into a char vector may be in this case a better idea than utilizing a common string.
My very first idea was to check all elements (stored in a char vector), step by step: When there's no lowercase after a capital letter, then I have found e.g. an element like Carbon 'C' instead of "Co" (Cobalt) or "Cu" (Copper). Basically, I've tried with the methods isupper(...), islower(...) or isalpha(...).
// first idea, but it seems to be definitely the wrong way
// read input characters from char vector
// check if element contains only one or two letters
// ... and convert them to a string, store them into a new vector
// ... finally, compute the molar mass elsewhere
// but how to deal with the numbers... ?
for (unsigned int i = 0; i < char_vec.size()-1; i++)
{
if (islower(char_vec[i]))
{
char arr[] = { char_vec[i - 1], char_vec[i] };
string temp_arr(arr, sizeof(arr));
element.push_back(temp_arr);
}
else if (isupper(char_vec[i]) && !islower(char_vec[i+1]))
{
char arrSec[] = { char_vec[i] };
string temp_arrSec(arrSec, sizeof(arrSec));
element.push_back(temp_arrSec);
}
else if (!isalpha(char_vec[i]) || char_vec[i] == '.')
{
char arrNum[] = { char_vec[i] };
string temp_arrNum(arrNum, sizeof(arrNum));
stoechiometr_num.push_back(temp_arrNum);
}
}
I need a simple algorithm which can handle with letters and numbers. There also may be the possibility working with pointer, but currently I am not so familiar with this technique. Anyway I am open to that understanding in case someone would like to explain to me how I could use them here.
I would highly appreciate any support and of course some code snippets concerning this problem, since I am thinking for many days about it without progress… Please keep in mind that I am rather a beginner than an intermediate.
This problem is surely not for a beginner but I will try to give you some idea about how you can do that.
Assumption: I am not considering Isotopes case in which atomic mass can be different with same atomic number.
Model it to real world.
How will you solve that in real life?
Say, if I give you Chemical formula: Fe(NO3)3, What you will do is:
Convert this to something like this:
Total Mass => [1 of Fe] + [3 of NO3] => [1 of Fe] + [ 3 of [1 of N + 3 of O ] ]
=> 1 * Fe + 3 * (1 * N + 3 * O)
Then, you will search for individual masses of elements and then substitute them.
Total Mass => 1 * 56 + 3 * (1 * 14 + 3 * 16)
=> 242
Now, come to programming.
Trust me, you have to do the same in programming also.
Convert your chemical formula to the form discussed above i.e. Convert Fe(NO3)3 to Fe*1+(N*1+O*3)*3. I think this is the hardest part in this problem. But it can be done also by breaking down into steps.
Check if all the elements have number after it. If not, then add "1" after it. For example, in this case, O has a number after it which is 3. But Fe and N doesn't have it.
After this step, your formula should change to Fe1(N1O3)3.
Now, Convert each number, say num of above formula to:
*num+ If there is some element after current number.
*num If you encountered ')' or end of formula after it.
After this, your formula should change to Fe*1+(N*1+O*3)*3.
Now, your problem is to solve the above formula. There is a very easy algorithm for this. Please refer to: https://www.geeksforgeeks.org/expression-evaluation/. In your case, your operands can be either a number (say 2) or an element (say Fe). Your operators can be * and +. Parentheses can also be present.
For finding individual masses, you may maintain a std::map<std::string, int> containing element name as key and its mass as value.
Hope this helps a bit.
I was given an assignment to make a program in cpp where you give it the width and length of the square and it will go into a pattern of *s and #s but I can't wrap my head around it. So I'm asking you people at stackoverflow to help me if you can. For reference when you would give an input of
6 6
the output would be:
######
#****#
#*##*#
#*##*#
#****#
######
and an input of
4 3
will give an output of:
####
#**#
####
This assignment is meant to help you understand how to use nested for loops.
for (int i = 0; ...)
{
for (int j = 0; ...)
{
// Do stuff here
}
}
Think about how a row can be formed by repeating a specific action. Then think about how a square can be formed by repeating the action of creating a row.
As for '*' vs '#', you always have access to both the x and y coordinate of the "current cell" you are about to print from inside the inner loop, because they are the counter variables for the two for loops. Remember: i and j are just arbitrary names. You could name those variables anything you want. You also have access to the length of each row and column, because they are given as input. Ask yourself: "what test can I make on the x or y coordinate that will tell me if it is the first or last column"?
Looping (or "iteration") is an extremely important concept in programming. Hopefully this gives you a hint in the right direction without giving too much away. :)
I want to count a value inside a template expression, in Xtend, without printing it out.
This is my code:
def generateTower(Tower in) {
var counter = 0.0;
'''
One Two Three Four
«FOR line : in.myTable»
«counter» «line.val1» «line.val2» «line.val3»
«counter = counter + 1»
«ENDFOR»
'''
}
So this will generate a table with four columns, whereas the first column is incremented starting at 0.0. The problem is, that «counter = counter + 1» is printed as well. But I want the expression above to just count up, without printing it out.
What could be the best solution to solve this problem?
You could use this simple and readable solution:
«FOR line : in.myTable»
«counter++» «line.val1» «line.val2» «line.val3»
«ENDFOR»
If you insist on the separate increment expression, use a block with null value. This works because the null value is converted to empty string in template expressions (of course you could use "" as well):
«FOR line : in.myTable»
«counter» «line.val1» «line.val2» «line.val3»
«{counter = counter + 1; null}»
«ENDFOR»
Although the first solution is the better. If you require complex logic in a template expression I recommend implementing it by methods not by inline code...
And finally, here is a more OO solution for the problem:
class TowerGenerator {
static val TAB = "\t"
def generateTower(Tower in) {
var counter = 0
'''
One«TAB»Two«TAB»Three«TAB»Four
«FOR line : in.myTable»
«generateLine(line, counter++)»
«ENDFOR»
'''
}
def private generateLine(Line line, int lineNumber) '''
«lineNumber»«TAB»«line.val1»«TAB»«line.val2»«TAB»«line.val3»
'''
}
Xtend is a full-fledged programming language. You can write Java-like expressions and templates. The problem there is that you're inside a triple quote (template), and everything you write there gets outputted. You can count inside the loop, but take into account that you're counting the elements in the in.myTable collection, and this can be obtained using in.myTable.length. So count could be calculated beforehand as in.myTable.length.
«{counter = counter + 1; null}» definitely worked. But as a recommendation, since it is java, writing it as «{counter++; null}» should do the trick as well. It helps because, you may need to modify your code and you can also put it in front as in: ++counter - by putting the operator first, the compiler takes a number, adds one to it before reading the value.
I got this error in Mathematica today:
Set::shape: "Lists {0,0,0,0,0,0,0,0,0,0} and {0,0,0,0,0,0,0,0,0,0,{1}} are not the same shape" >>
And after 3 of those :
General::stop : Further output of Set::shape will be suppressed during this calculation. >>
I am confused as to why I cannot append a "1" to my list of zeros. Is this because I cannot edit the list that is passed into the function? If so, how could I edit that list and somehow return or print it?
Here is my full code:
notFunctioningFunction[list_] := (For[i = 1, i < 10, i++, list = Append[list, {1}]];
Print[list])
list = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
notFunctioningFunction[list]
The reason why I am appending a "{1}" is because in my function, I am solving an equation, and getting the value of the variable which outputs {1}. Here is my code for that :
varName / . Solve[ function1 == function2 ]
Obviously I am a beginner with Mathematica so please be patient :)
Thanks,
Bucco
Append needs to take one list and one element. Like so:
Append[{1,2,3,4},5]
If you have two lists, you can use Join. Like so:
Join[{1,2,3,4},{5}]
Both of these will yield the same result: {1,2,3,4,5}.
Dear Mathematica beginner.
First, when you use something like
{a,b} = {c,d,e};
in Mathematica, between two lists, the program has a difficulty because this is a construct used to assign values to variables, and it requires (among other things) the two lists to be equal.
If what you want is just to add a "1" to an existing and named list, one at a time, the best construct is:
AppendTo[list, 1];
(this construct will modify the variable 'list')
or
list = Join[list, {1}];
Second: about the error messages, they are printed 3 times by default in an evaluation, then muted so that a long list of identical error messages does not clutter your display.
Third, if what you need is adding 10 1s to a list, there is no need to construct that in a loop. You can do that in one pass:
list = Join[list, Table[1, {10}]]
or, more cryptic for beginners
list = Join[list, Array[1&, 10]]