Use 2 nested loops to print out the following shapes - loops

Use 2 nested loops to print out the following shapes

Related

print interactive graph (girafe object) inside loop in rmarkdown

I have a list of girafe objects, created using girafe() from the package ggiraph. These are essentially interactive ggplot objects.
I would like to loop over the list of girafe objects and print these plots in an rmarkdown file.
Using results = "asis" in the code chunk, my attempts have been along the lines of:
if ("chart.ls" %in% names(current)) {
for (i in seq_along(current$chart.ls)){
knitr::knit_print(current$chart.ls[[i]])
}
}
Further, the below code will print the plot as expected
if ("chart.ls" %in% names(current)) {
knitr::knit_print(current$chart.ls[[2]])
}
I understand there are alternative ways to print graphs from inside a loop, but these methods don't seem to work for interactive graphs.
Is there a way to print girafe objects from inside a loop, without using external packages? (e.g. knitrContainer)
I have a workaround, which is to use child Rmds. Where I can loop over the object, then reference a child.rmd which then prints it. But hoping for a real solution.

Looping thorught array from JSON Swift

This is the structure i made to decode the data i get from JSON
This the main code where i call the array result and tap into the tile and print out the title.
So my question is that result is array and it contain 14 items and in that 14 item there are 10 additional thing and title is one of them, so i want to print the title using FOR loop .
This the JSON file i am trying to parse
One way you could iterate over each of your items is using the for X in Y pattern.
for movie in decodeData.results {
print(movie.title)
print(movie.poster_path)
}
This pattern will iterate through each result in your array and assign it to the variable name you specify in the for statement.
You can learn more here: Swift Control Flow

Stateflow chart and C Action Language. Set array values for give in output a vector in single step

i'm sorry, maybe is a stupid question, but i have issue to set my array variable in a stateflow chart by using C Action Language.
Usually in c language i can put in my static array all the values with " A[]={1,3,2}; " , but in stateflow chart, with C ActionLanguage how i can it? (in matlab language work, but i need in C)
I've defined my variable with output scope (becouse it is), setted the size as 3 (i want an array of 3 elements), set First index as "0" and type int8.
If a want in output the array "1,2,3" i write " gates=[1,2,3]; " , i've tried also "gates=[1 2 3]", etc, but i receive syntax error.
What are my errors?
i've read in this link: https://it.mathworks.com/help/stateflow/ug/operations-for-vectors-and-matrices.html
Assign Values to All Elements of a Matrix In charts that use MATLAB as
the action language, you can use a single action to specify all of the
elements of a vector or matrix. For example, this action assigns each
element of the 2-by-3 matrix A to a different value: A = [1 2 3; 4 5
6]; In charts that use C as the action language, you can use scalar
expansion to set all of the elements of a vector or matrix to the same
value. Scalar expansion converts scalar data to match the dimensions
of vector or matrix data. For example, this action sets all of the
elements of the matrix A to 10 A=10;
but not explain how set different values in all the array, and also i need that the output from the stateflow chart is in a single step :(.
in the image there is a step with a syntax error. Someone can explain me how i can solve?
Stateflow chart screenshot
thankyou!
Well, there are 2 ways I would do it:
Define the values for ex: g1 = [1 0 0]; g0 = [0,0,0] in MATLAB workspace. And then in your stateflow use the values like gates = ml.g1; This works with C action language.(attached image gives a preview of this method)
Implement a for loop(maybe a graphical function or a MATLAB function) inside your chart, which is used to assign values to your output array.
Of the two, I think the first method is easier to deal with, if the no of values the variable 'gates' takes is limited. Else, go for method 2.

How to append two numpy arrays if a for loop after loading data?

I have two numpy arrays. I want to append then along zero axis. If I use np.append command it demands two arguments. Whereas the following code loads first array and then second array. How I can implement np.append command in this case? I read different commands like np.concatenate and np.vstack but I don't know how to implement it in for loop. Can someone please guide me about this.
`list=[1,2]
for i in list:
s=np.load("%s.npy"%i)
r=np.append(s,axis=0)
print(s)
print(s.shape)`
You can load everything in a list of arrays and concatenate that list together to one array:
r = np.concatenate([np.random.rand(5, 5) for i in [1, 2]], axis=0)

What should I use for a collection of different objects in matlab?

This is illegal in Matlab
a = [[1];[2 3]]
In languages that allow this, this is called nested arrays.
I found a way of doing the same in Matlab:
a = {[1];[2 3]}
What is this called?
How initialize such a variable with a fixed size (say 100) without having to write much code?
It is called a cell array.
You initialize it using the command cell
cellArray = cell(3,2); %# this makes a 3-by-2 cell array
An alternative way to store collections of different objects is the struct, which you'd initialize like this
myStruct = struct('firstField',1,'secondField',[2 3])
The advantage of structs over cells is that the fields are named, which makes it a lot easier to deal with and document. Cells can be very convenient for storing data if you want to manipulate the data often, because you can for example use cellfun with them. I find myself often using cells to keep data inside a function, but using structures (or objects) to pass data between functions.
Also, if you have a list of numbers and want to distribute them to elements of the cell array, you can use num2cell, which puts every element of the array separately into an element of the cell array, or mat2cell, in case you want to split the array unevenly.
a = {1,[2 3]}
is equivalent to
b = mat2cell([1 2 3],[1 1],[1 2]);
Alternatively I could discover the meaning of the curly brackets by typing
help paren
Which outputs:
{ } Braces are used to form cell arrays. They are similar to
brackets [ ] except that nesting levels are preserved.
{magic(3) 6.9 'hello'} is a cell array with three elements.
{magic(3),6.9,'hello'} is the same thing.
{'This' 'is' 'a';'two' 'row' 'cell'} is a 2-by-3 cell array.
The semicolon ends the first row. {1 {2 3} 4} is a 3 element
cell array where element 2 is itself a cell array.
Braces are also used for content addressing of cell arrays.
They act similar to parentheses in this case except that the
contents of the cell are returned.
Some examples:
X{3} is the contents of the third element of X.
X{3}(4,5) is the (4,5) element of those contents.
X{[1 2 3]} is a comma-separated list of the first three
elements of X. It is the same as X{1},X{2},X{3} and makes sense
inside [] ,{}, or in function input or output lists (see LISTS).
You can repeat the content addressing for nested cells so
that X{1}{2} is the contents of the second element of the cell
inside the first cell of X. This also works for nested
structures, as in X(2).field(3).name or combinations of cell arrays
and structures, as in Z{2}.type(3).
That's a cell array. Avoid them unless you really need them, because they're a pain to work with, they're much slower, and the syntax is a horrible, inconsistent, bolt-on kludge.

Resources