is there a built in way to create a list of consecutive integers? - netlogo

I'm looking for a built in way to create a list of consecutive integers: 1 to n.
IE: [0 1 2 3 4 5 6 7 8]
Does it exist? Currently I'm manually creating the list, which is wasteful.
Manual method: let group-list [0 1 2 3 4 5 6 7 8]

Found the answer:
show n-values 9 [?] will display [0 1 2 3 4 5 6 7 8]
show n-values 9 [? + 1] will display [1 2 3 4 5 6 7 8 9]

Related

How to access n-D matrix with n index vectors? [duplicate]

This question already has answers here:
MATLAB: how to pass in the diagonal of a matrix as an argument in another matrix?
(2 answers)
Closed 6 years ago.
I have a matrix
A = repmat(1:7,7,1);
I have index vectors
idx1 = [1 3 5];
idx2 = [1 3 5];
I want to access A at the 2d coordinates denoted by idx1(i),idx2(i).
When I do
A(idx1,idx2) = 0;
I get for each element in idx 1, all the elements in idx2 as well.
I want only the corresponding elements to be assigned the zero value.
Again: I get
A =
0 2 0 4 0 6 7
1 2 3 4 5 6 7
0 2 0 4 0 6 7
1 2 3 4 5 6 7
0 2 0 4 0 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
but I want
A =
0 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 0 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 0 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
How to achieve this?
Thanks
The easiest way is probably to use sub2ind to generate the linear indices needed to index into A:
linear_ind = sub2ind(size(A),idx1,idx2);
A(linear_ind) = 0;

How to get range of elements in a list in KDB?

For example, I have this list:
test:(8;12;15;19;10)
How may I select elements 2 to 4?
When I try list[2;4] it doesn't work for me.
indexing a list is by far the fastest way.
q)a
8 1 9 5 4 6 6 1 8 5 4 9 2 7 0 1 9 2 1 8 8 1 7 2 4 5 4 2 7 8 5 6 4 1 3 3 7 8 2..
q)\t do[100000;2 3 sublist a]
109
q)\t do[100000;a 2 3 4]
15
So just follow your list with a list of indexes. BTW you can create indexes with til
q)til 2
0 1
q)2+til 2
2 3
You can use sublist for this
test:(8;12;15;19;10);
2 3 sublist test
This will return three elements from the list starting at index 2.
As answered by Manish, til is the best bet here. You can define a simple function range using til to give the index range :
q)test:(8;12;15;19;10)
q)range:{x+til 1+abs[y-x]} //include the start and end index
q)test range[2;4]
15j, 19j, 10j

Adding value to a cell element specified by array

I am looking for a vectorized solution!
I have two arrays for example:
idx=[1 1 1 2 2 3 3 4 4 4 5 5 6 7 8 8 8 9 9]; %%//Integers,sorted
val=[1 4 8 2 5 3 9 1 4 8 2 5 6 7 1 4 8 3 9]; %%//respective Values (could be anything)
Now i want to create a cell array which contains in his elements specified by idx the according values of val. So the result should be a [9x1] cell with:
[1 4 8]
[2 5]
[3 9]
[1 4 8]
[2 5]
[6]
[7]
[1 4 8]
[3 9]
I know I could loop over the values from 1 to 9 and use horzcat while idx is equal to my loop index but I am looking for a vectorized solution. Reason is, that I am trying to change a loop solution of a problem to vectorized solution yet I am stuck here
Use accumarray:
out = accumarray(idx(:),val(:),[],#(x){x},{});
mat2cell(val,1,diff([0,find(diff(idx)),numel(idx)]))
Maybe someone finds a possibility to get rid of the find, then it's probably faster.

Remove zeros from matrix in matlab

I have a question and I hope it is not duplicate.
First, I have let say the following matrix:
A=[2 2 2 0 0
1 2 3 0 0
4 5 7 2 0]
I want to remove the zeros from A and return:
A=[2 2 2
1 2 3
4 5 7]
When I do
A(A==0)=[]
I get
A=[2 2 2 1 2 3 4 5 7]
Second, if instead of zeros I want to remove the elements that are greater than something. For example if I want to remove all elements greater than 6 (>6) of the following matrix B:
B=[2 2 2 5 3
1 2 3 6 8
4 5 7 2 1]
I get
A=[2 2 5
1 2 6
4 5 2]
P.S. I know how to do it using loops.
First problem solution
A(:,find(all(A,1)))
Second problem solution
B(:,~any(B>6,1))

Find the maximum value from the each set

I have 3 sets of data as shown in below:
A=[3 1 4 2;7 9 8 3;7 5 3 6;4 1 9 3]
B=[1 0 4 5;7 7 1 3;4 7 6 5;2 2 1 9]
C=[9 1 3 7;9 6 5 5;1 4 3 2;0 3 2 1]
I need to find out the maximum value when comparing with each other.
for instance, for matrix [1x1] from each set,
A=3,B=1,C=9,thus maximum number is 9
for matrix [1x2],maximum value=1 and so on..
so the
final result =[9 1 4 7;9 9 8 5;7 7 6 6;4 3 9 9]
Any suggestion to solve this problem? Thanks!
You can use max. For the case with 3 matrices, just use
max(A, max(B, C))
If you have more than three matrices, writing those max statements can get tiring, so you would use cat before taking the maximum
max(cat(3, A, B, C, D, E), [], 3)
You could do like this:
A=[3 1 4 2;7 9 8 3;7 5 3 6;4 1 9 3];
A = reshape(A,[1,numel(A)]);
B=[1 0 4 5;7 7 1 3;4 7 6 5;2 2 1 9] ;
B = reshape(B,[1,numel(B)]);
C=[9 1 3 7;9 6 5 5;1 4 3 2;0 3 2 1];
C = reshape(C,[1,numel(C)]);
D = [A;B;C];
for ii = 1:size(D,2)
res(1,ii) = max(D(:,ii));
end
res = reshape(res,[4,4]);