when I use :
display {(k,m) in YBUS : k==1 }
set {(k,m) in YBUS: k == 1} := (1,1) (1,2) (1,5);
I have variable G for each YBUS elements so I want to display the value of G(1,1) , G(1,2) and G(1,5) so I write :
display {(k,m) in YBUS : k==1 } G(k,m) ;
and it does not work. can anyone help me with the correct format that I should use.
You should use square brackets instead of parentheses in subscript:
display {(k, m) in YBUS: k == 1} G[k, m];
Related
I'm trying to learn some python here . I'm trying to solve a task where the input consists of lines of "" and "." The "" are forming rectangles down the lines and my job is to identify and count them . I think of an approach: I will find all the "*" symbols in the lines get their indexes and put them in nested lists which I will compare .
And here is my problem :
size = input()
height = int(size[0])
width = int(size[2])
draw = []
x = []
y=[]
for i in range(height):
draw.append(input())
for el in draw:
for ch in el:
if ch == "*":
y.append(el.index(ch))
x.append(y)
y=[]
print(draw)
For some reason unknown to me the "y.append(el.index(ch))" always appends "0" to the list . I'm sure there is a reason for that , but I can't seems to figure it out . I'm looping over the list so the index should be different every time it goes over . What am I doing wrong ???
Also if there is some better approach(I'm sure there is) a hint will be appreciated.
Thanks !!!
If it matches the string then increment else continue
meaning if x = ["you","me","us","then"] and y = ["hi","king","you","you","thai","you"] the code should take string you compare with all the elements in y and then increment a variable if it matches and return 3
Note: the code should not stop if it matches once with you it should search till end of the elements?
x = ["you","me","us","then"]
y = ["hi","king","you","you","thai","you"]
word_count = {}
for each_word in x :
word_count[each_word] = y.count(each_word)
print(dict)
#result : {'you': 3, 'me': 0, 'us': 0, 'then': 0}
if this is not the answer you are looking for, Please explain the question by providing sample result you are expecting.
I'd like to replace a character in a string with another character, by first sampling by the character. I'm having trouble having it print out the character instead of the index.
Example data, is labelled "try":
L 0.970223325 - 0.019851117 X 0.007444169
K 0.962779156 - 0.027295285 Q 0.004962779
P 0.972704715 - 0.027295285 NA 0
C 0.970223325 - 0.027295285 L 0.00248139
V 0.970223325 - 0.027295285 T 0.00248139
I'm trying to sample a character for a given row using weighted probabilities.
samp <- function(row) {
sample(try[row,seq(1, length(try), 2)], 1, prob = try[row,seq(2, length(try), 2)])
}
Then, I want to use the selected character to replace a position in a given string.
subchar <- function(string, pos, new) {
paste(substr(string, 1, pos-1), new , substr(string, pos+1, nchar(string)), sep='')
}
My question is - if I do, for example
> subchar("KLMN", 3, samp(4))
[1] "KL1N"
But I want it to read "KLCN". As.character(samp(4)) doesn't work either. How do I get it to print out the character instead of the index?
The problem arises because your letters are stored as factors rather than characters, and samp is returning a data.frame.
C is the first level in your factor so that is stored as 1 internally, and as.character (which gets invoked by the paste statement) pulls this out when working on the mini-data.frame:
samp(4)
V1
4 C
as.character(samp(4))
[1] "1"
You can solve this in 2 ways, either dropping the data.frame of the samp output in your call to subchar, or modifying samp to do so:
subchar("KLMN", 3, samp(4)[,1])
[1] "KLCN"
samp2 <- function(row)
{ sample(try[row,seq(1, length(try), 2)], 1, prob = try[row,seq(2, length(try), 2)])[,1]
}
subchar("KLMN",3,samp2(4))
[1] "KLCN
You may also find it easier to sample within your subsetting, and you can drop the data.frame from there:
samp3 <- function(row){
try[row,sample(seq(1,length(try),2),1,prob=try[row,seq(2,length(try),2)]),drop=TRUE]
}
I have two lists :
a = [1,2,3]
b = ["?",1,2,"?",4,"?"]
In the second list, I need to replace the first "?" with first element of a(i.e a[0]) and second "?" with a[1] and so on(provided that the number of "?" = size of a) and the result as modified b.
How I can do this groovy-er way?
Thanks in advance.
Some simple solutions:
This returns the result in a new list (you can assign this result to the b variable)
def i = 0
b.collect { it == "?" ? a[i++] : it }
This modifies the list referenced by b
a.each { b[b.indexOf("?")] = it }
for i=1:N
f(i) = 'f'+i;
end
gives an error in MatLab. What's the correct syntax to initialize an array with N strings of the pattern fi?
It seems like even this is not working:
for i=1:4
f(i) = 'f';
end
You can concatenate strings using strcat. If you plan on concatenating numbers as strings, you must first use num2str to convert the numbers to strings.
Also, strings can't be stored in a vector or matrix, so f must be defined as a cell array, and must be indexed using { and } (instead of normal round brackets).
f = cell(N, 1);
for i=1:N
f{i} = strcat('f', num2str(i));
end
For versions prior to R2014a...
One easy non-loop approach would be to use genvarname to create a cell array of strings:
>> N = 5;
>> f = genvarname(repmat({'f'}, 1, N), 'f')
f =
'f1' 'f2' 'f3' 'f4' 'f5'
For newer versions...
The function genvarname has been deprecated, so matlab.lang.makeUniqueStrings can be used instead in the following way to get the same output:
>> N = 5;
>> f = strrep(matlab.lang.makeUniqueStrings(repmat({'f'}, 1, N), 'f'), '_', '')
f =
1×5 cell array
'f1' 'f2' 'f3' 'f4' 'f5'
Let me add another solution:
>> N = 5;
>> f = cellstr(num2str((1:N)', 'f%d'))
f =
'f1'
'f2'
'f3'
'f4'
'f5'
If N is more than two digits long (>= 10), you will start getting extra spaces. Add a call to strtrim(f) to get rid of them.
As a bonus, there is an undocumented built-in function sprintfc which nicely returns a cell arrays of strings:
>> N = 10;
>> f = sprintfc('f%d', 1:N)
f =
'f1' 'f2' 'f3' 'f4' 'f5' 'f6' 'f7' 'f8' 'f9' 'f10'
Using sprintf was already proposed by ldueck in a comment, but I think this is worth being an answer:
f(i) = sprintf('f%d', i);
This is in my opinion the most readable solution and also gives some nice flexibility (i.e. when you want to round a float value, use something like %.2f).
according to this it looks like you have to set "N" before trying to use it and it looks like it needs to be an int not string? Don't know much bout MatLab but just what i gathered from that site..hope it helps :)
Try the following:
for i = 1:4
result = strcat('f',int2str(i));
end
If you use this for naming several files that your code generates, you are able to concatenate more parts to the name. For example, with the extension at the end and address at the beginning:
filename = strcat('c:\...\name',int2str(i),'.png');