what are the different ways you can optimize following function? - coding-style

what are the different ways you cawhat are the different ways you can optimize following function?n optimize following function?
if(a==1 || a==5 || a==-1 || a==7 ||a==-8 ||a==11 ||a==6 ||a=11 )
and again i want to add some more if condition in this function?
What are the different approach you can suggest?

You can do like this-------
1st- Create an int array(number array)
2nd- Add all the numbers you want to check
3rd- Finally check whether number 'a' is in the array. ex- array[].includes(a);

Related

Array of value from multiple conditions

I wanna return an array of value from multiple conditions.
Currently formula is set to accept one condition.
https://docs.google.com/spreadsheets/d/1pgVlBWYKWtT6AEPyRtZmAYdOXBCjYG_B7pdlvCqYq0A/edit?usp=sharing
The desired result is showed in the ad hoc worksheet
Edit : initial problem solved by player0. Thanks !
Previous post
I'm using curly brace to return an array of value with the IF formula. This works well. I wanted to use IFS function because i wanted to use more conditions.
With a similar table, only the 1st number is returned form the array.
I don't understand why.
https://docs.google.com/spreadsheets/d/1pgVlBWYKWtT6AEPyRtZmAYdOXBCjYG_B7pdlvCqYq0A/edit?usp=sharing
Thanks !
delete range E2:L and use this in E2:
=ARRAYFORMULA(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(
IF((E1:L1=B2:B11)+(E1:L1=C2:C11)+(E1:L1=D2:D11);
"1;2;3;4;5;6;7"; ";");;9^9)); ";"; 1; 0)))

Writting many Equations for NDSolve

I'm trying to write the master equations for genetic networks, as they are many equations I'm trying to make a table for writing all of them at time. However, I don't know how to adjust the boundaries, I mean:
I wrote a matrix with all the variables that I need:
p={{p11,p12},{p21,p22}}
Then I wrote a table for creating the differential equations:
Table[p[[i,j]]'[t]== p[[i-1,j]][t]+p[[i,j-1]][t]+p[[i+1,j]][t]+p[[i,j+1]][t],{i,1,2},{j,1,2}]
However the part p[[i-1,j]] when i=1 is p[[0,1]]but it doesn't exist and I need to put 0 instead of this but I dont not how. I tried with If but it doesn't work well. What can i do?
Will this work for you?
pf[i_,j_]:=If[i<1||i>2||j<1||j>2,0,p[[i,j]][t]];
Table[p[i,j]'[t]== pf[i-1,j]+pf[i,j-1]+pf[i+1,j]+pf[i,j+1],{i,1,2},{j,1,2}]
which returns
{{p[1, 1]]'[t] == p[[1,2]][t] + p[[2,1]][t], p[1, 2]]'[t] == p[[1,1]][t] + p[[2,2]][t]},
{p[2, 1]]'[t] == p[[1,1]][t] + p[[2,2]][t], p[2, 2]]'[t] == p[[1,2]][t] + p[[2,1]][t]}}

XQuery/XPath "except" operation - Select part of sequence that is not in other sequence

I have a pretty simple example but I am just learning and can't find a solution for the following:
Given 2 sequences, being
<emp>10</emp>
<emp>42</emp>
<emp>100</emp>
and another sequence
<emp>10</emp>
<emp>42</emp>
Want i want to do is: Compare the sequences and return the part of sequences that is in the first, but not in the 2nd sequence, being <emp>100</emp> in this case.
I was thinking about an "except"-operation, but can't figure out how to make it working.
Help greatly appreciated.
The except expression operates on node identity, not node value. What I think you want is a value comparison over your sequences. For example:
let $seq1 :=
(<emp>10</emp>,
<emp>42</emp>,
<emp>100</emp>)
let $seq2 :=
(<emp>10</emp>,
<emp>42</emp>)
return $seq1[not(. = $seq2)]
=>
<emp>100</emp>

How to get an array of values where columns match multiple criteria

I have a table of data similar to:
where I'd like to get just the shapes which match a set of given criteria (in this case week=2 and colour=blue).
I can return the first result using index and match like:
=ArrayFormula(INDEX(C2:C14,MATCH($F$1&$F$2,A2:A14&B2:B14,0)))
but I'd like to return the all matching values (eg square and triangle) in to the range F3:Fsomething. This would preferably be done using a formula that returns a range and isn't "copied-down", as a list of all possible shapes isn't known beforehand.
How can I modify this formula to achieve this?
See if this works:
=FILTER (C2:C14, B2:B14=F2, A2:A14=F1)
to do multiple criteria you want to use * like so
=FILTER (C2:C14, (B2:B14=F2) * (A2:A14=F1))
and if you want the results all in the same cell with a delimiter, use TEXTJOIN
=TEXTJOIN([DELIMETER],[IGNORE EMPTY TEXT],text1)
=TEXTJOIN(", ",TRUE,FILTER(C2:C14,(B2:B14=F2)*(A2:A14=F1)))

An efficient, optimized code for matching rows in a huge matrix

I have a huge matrix on which I need to do some matching operation. Here's the code I have written and it works fine, but I think there is some room to make it more optimized or write another code that does the matching in less time. Could you please help me with that?
rowsMatched = find(bigMatrix(:, 1) == matchingRow(1, 1)
& bigMatrix(:, 2) == matchingRow(1, 2)
& bigMatrix(:, 3) == matchingRow(1, 3))
The problem with this code is that I cannot use && operand. So, in case one of the columns do not match, the program still checks the next condition. How can I avoid this?
Update: Here's the solution to this problem:
rowsMatched = find(all(bsxfun(#eq, bigMatrix, matchingRow),2));
Thank you
You can use BSXFUN to do it in a vectorized manner:
rowsMatched = find(all(bsxfun(#eq, bigMatrix, matchingRow),2));
Notice that it will work for any number of columns, just matchingRow should have the same number of columns as bigMatrix.

Resources