Create a specific number pattern using matlab / octave - matlab

How can I create a specific number pattern? Where the last number starts the 2nd pattern section.
Example:
I want to follow / repeat the pattern 1,5,4
1st pattern in section 1,5,4
2nd pattern in section 5,9,8
3rd pattern in section 9,13,12
final array would be = [1,5,4,5,9,8,9,13,12,...]
note: this is just a simple example the pattern will be about 100,000 + numbers
I know about repmat and cumsum but combining these two didn't work
PS: I'm using Octave 4.2.2 which is similar to Matlab

x=reshape([1;5;4]+[0:4:n*4],1,[])
When you take a look at your question, each row is [1;5;4] incremented by a multiple of 4. That is what above code does, adds [1;5;4] with a multiple of 4. Finally the result is reshaped to a vector.

Related

append an atom with exisiting variables and create new set in clingo

I am totally new in asp, I am learning clingo and I have a problem with variables. I am working on graphs and paths in the graphs so I used a tuple such as g((1,2,3)). what I want is to add new node to the path in which the tuple sequence holds. for instance the code below will give me (0, (1,2,3)) but what I want is (0,1,2,3).
Thanks in advance.
g((1,2,3)).
g((0,X)):-g(X).
Naive fix:
g((0,X,Y,Z)) :- g((X,Y,Z)).
However I sense that you want to store the path in the tuple as is it is a list. Bad news: unlike prolog clingo isn't meant to handle lists as terms of atoms (like your example does). Lists are handled by indexing the elements, for example the list [a,b,c] would be stored in predicates like p(1,a). p(2,b). p(3,c).. Why? Because of grounding: you aim to get a small ground program to reduce the complexity of the solving process. To put it in numbers: assuming you are searching for a path which includes all n nodes. This sums up to n!. For n=10 this are 3628800 potential paths, introducing 3628800 predicates for a comparively small graph. Numbering the nodes as mentioned will lead to only n*n potential predicates to represent the path. For n=10 these are just 100, in comparison to 3628800 a huge gain.
To get an impression what you are searching for, run the following example derived from the potassco website:
% generating path: for every time exactly one node
{ path(T,X) : node(X) } = 1 :- T=1..6.
% one node isn't allowed on two different positions
:- path(T1,X), path(T2,X), T1!=T2.
% there has to be an edge between 2 adjascent positions
:- path(T,X), path(T+1,Y), not edge(X,Y).
#show path/2.
% Nodes
node(1..6).
% (Directed) Edges
edge(1,(2;3;4)). edge(2,(4;5;6)). edge(3,(1;4;5)).
edge(4,(1;2)). edge(5,(3;4;6)). edge(6,(2;3;5)).
Output:
Answer: 1
path(1,1) path(2,3) path(3,4) path(4,2) path(5,5) path(6,6)
Answer: 2
path(1,1) path(2,3) path(3,5) path(4,4) path(5,2) path(6,6)
Answer: 3
path(1,6) path(2,2) path(3,5) path(4,3) path(5,4) path(6,1)
Answer: 4
path(1,1) path(2,4) path(3,2) path(4,5) path(5,6) path(6,3)
Answer: 5
...

How can I reduce the number of rows of a matrix by half in matlab?

What is the best way to reduce the number of rows of a matrix by half in matlab?
What is the following command doing?
mymatrix = mymatrix(1:2:end,:);
Is there any better way available?
Short answer this is taking every second row of the matrix mymatrix starting with the first one (all odd-rows) and yes that is probably the easiest way. Added clarification based on comment from #Sardar_Usama
Longer version
end is matlab internal command that refers to the end of the array in the given dimension. roughly equivalent to size(var,dim).
so actually what mymatrix(1:2:end,:) can be re-written to mymatrix(1:2:size(mymatrix,1),:). Now if you actually look at 1:2:size(mymatrix,1) these are the rows that you are selecting.1, 3, 5, etc. You can actually specify whichever rows you want there, here are some examples.
1:floor(end/2); % first 'half'
floor(end/2)+1:end; % second 'half'
1:3:end; % every third element
1:2:floor(end/2); % every second element in the first 'half'
Added floor() to avoid problems for odd number lengths. In that case 'half' is not exactly half but rather roughly half. Alternatively ceil() depending on how you would like to define half for odd lengths.

Selecting columns/rows of a matrix in Julia

this is a very basic problem but I didn't find any hints on it. Let's say I have a 2x4 matrix and I want to reduce the dimension of the matrix to only these columns that are in the sum larger than 1:
A=rand(2,4)
ind = sum(A,1).>1
That gives me an indicator of the columns I want to retain. Naively one would assume that I can do that:
A[:,ind]
which doesn't work as ind is a BitArray and only for Bool Arrays this is allowed, i.e., the following works
A[:,[true,true,false,true]]
in return, the following does work:
A[A.>0.5]
But it returns a vector of filtered elements.
What is the logic behind this and how do I solve my problem?
As noted in the comments, this is fixed by using a version of Julia which is >=v0.4.

Matlab : How to extract elements in a uniform interval?

I have a (5160 X 4) matrix.
I want to extract just (1,1),(41,1),(81,1),(121,1)........ in a uniform interval, only from the first column of the matrix.
Assuming that data is your matrix, you can do this:
A = data(1:40:5160,1);
The 1:40:5160 will create an array such that it starts at 1, and goes up in increments of 40 as much as possible up until 5160. Once you create 1:40:5160, you can use this array and access the corresponding rows, and you are accessing the first column using the index of 1 for the second parameter. Actually, the last row that gets extracted is 5121. We aren't able to go up to 5161 due to the fact that your matrix has 5160 rows, and we have also specified 5160 as the ending of the indexing.
NB: This is very basic MATLAB syntax. Any standard MATLAB tutorial should teach you this.

How to add a repeating pattern to vector with different length in Matlab

Question
Suppose I have two vectors of arbitrary length. Lets call one pattern and the other series. Now I want to add my repeated pattern to my series in an automatic way.
Typically one can assume that pattern is shorter than series, but it would be nice if the alternate way also worked. In this case just the first few values of pattern should be used.
Example
pattern = 1:3;
series = 1:10;
Should give
2 4 6 5 7 9 8 10 12 11
What have I found so far?
I have searched around but did not find an elegant way to achieve what I want.
The easiest solution I found uses padarray, however I do not have this available
My own solution,that I don't consider to be elegant, is using repmat to repeat the pattern a sufficient amount of times and then cutting of the end.
You could use indexing instead of repmat:
result = series + pattern([mod(0:(numel(series) - 1), numel(pattern)) + 1]);