How to access adjacent elements from a current starting position? - arrays

I have a matrix:
E = [ 3 6 3 7 2 ; 3 8 3 7 2; 1 9 5 4 1; 7 5 1 6 9; 10 8 4 3 10 ]
If I started at E(3,3) = 5, how do I access the adjacent numbers to the east (or west). For example, I wanted to access the 3 elements to the east of E(3,3), which are 7, 4 and 6.

% define matrix and point
E = [ 3 6 3 7 2 ; 3 8 3 7 2; 1 9 5 4 1; 7 5 1 6 9; 10 8 4 3 10 ];
Raw=3;
Col=3;
% find the value of the point, 3 east values and 3 west values
Point=E(Raw,Col)
EastVal=E(max(1,Raw-1):min(size(E,1),Raw+1),Col+1)
WestVal=E(max(1,Raw-1):min(size(E,1),Raw+1),Col-1)
Produce:
Point =
5
EastVal =
7
4
6
WestVal =
8
9
5

Related

J: Coordinates with specific value

Let's say we have array
0 1 2 3 4 5 8 7 8 9
There are two indexes that have value 8:
(i.10) ([#~8={) 0 1 2 3 4 5 8 7 8 9
6 8
Is there any shorter way to get this result? May be some built-in verb.
But more important. What about higher dimensions?
Let's say we have matrix 5x4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
I want to find out what are coordinates with value 6.
I want to get result such (there are three coordinates):
4 1
3 2
2 3
It's pretty basic task and I think it should exist some simple solution.
The same in three dimensions?
Thank you
Using Sparse array functionality ($.) provides a very fast and lean solution that also works for multiple dimensions.
]a=: 5 ]\ 1 + i. 8
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
6 = a
0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
4 $. $. 6 = a
1 4
2 3
3 2
Tacitly:
getCoords=: 4 $. $.
getCoords 6 = a ,: a
0 1 4
0 2 3
0 3 2
1 1 4
1 2 3
1 3 2
Verb indices I. almost does the job.
When you have a simple list, I.'s use is straightforward:
I. 8 = 0 1 2 3 4 5 8 7 8 9
6 8
For higher order matrices you can pair it with antibase #: to get the coordinates in base $ matrix. Eg:
]a =: 4 5 $ 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
I. 6 = ,a
9 13 17
($a) #: 9 13 17
1 4
2 3
3 2
Similarly, for any number of dimensions: flatten (,), compare (=), get indices (I.) and convert coordinates (($a)&#:):
]coords =: ($a) #: I. 5 = , a =: ? 5 6 7 $ 10
0 0 2
0 2 1
0 2 3
...
(<"1 coords) { a
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
By the way, you can write I. x = y as x (I.#:=) y for extra performance. It is special code for
indices where x f y

How to do print formatting in Python with chunks of strings?

I'm having some trouble with formatting the pyramid. I've tried to use format when printing from the loop but that didn't seem to work and just breaks the program. What would be different ways to format the output. The only trouble that I am having is when I am printing 10 and up when there's double digits. What would be the best approach formatting the printing output? I've tried variety of ways but couldn't make formatting work within the loop from documentation
https://docs.python.org/3.5/library/string.html#formatstrings
Here is the script:
userinput = int(input("Enter the number of lines: " )) # User input of the total number of lines
userinput = userinput + 1 # adding a value of 1 additionally with the user input to make numbers even
for i in range(1, userinput): # Loop through lines from 1 to userinput
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(" ", end = " ")
for j in range(i, 0, -1): # printing number decreasing from the line number j to 1
print(j, end = " ")
for j in range(2,i + 1): # Printing number increasing from 2 to line number j
print(j, end = " ")
print()
j += 1
The output when its less than 10
Enter the number of lines: 9
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
The output when it's 15 or more:
Enter the number of lines: 15
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
When I have reserved an extra space for 10 and up, here is what my outout looks like: (The dots were used to distinguish from empty space, all I did was added a " " quotes in the beginning of the print.
Enter the number of lines: 12
. . . . . . . . . . . . 1
. . . . . . . . . . . 2 1 2
. . . . . . . . . . 3 2 1 2 3
. . . . . . . . . 4 3 2 1 2 3 4
. . . . . . . . 5 4 3 2 1 2 3 4 5
. . . . . . . 6 5 4 3 2 1 2 3 4 5 6
. . . . . . 7 6 5 4 3 2 1 2 3 4 5 6 7
. . . . . 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
. . . . 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
. . . 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
. . 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
. 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
Here is what I've tried changing by adding aditional space
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(".", end = " ")
for j in range(i, 0, -1): # printing number decreasing from the line number j to 1
print(" ", j, end = "")
for j in range(2,i + 1): # Printing number increasing from 2 to line number j
print(" ", j, end = "")
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(" ", end = " ")
Here is the ideal output of what I am trying to accomplish:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Thank you!
The things to consider for this problem are
The length of the largest number.
The length of the current number being printed.
The difference in lengths.
In order to correctly space everything, you're going to need to print extra
spaces after the numbers with less digits (to compensate for the extra digits in the larger number).
For example, if you have a row that contains the number 10, in order to correctly space the other smaller numbers, you're going to need to use extra spaces to compensate for that second digit in the number 10.
This solution works for me.
userinput = int(input("Enter the number of lines: " ))
userinput = userinput + 1
# Here, you can see I am storing the length of the largest number
input_length = len(str(userinput))
for i in range(1, userinput):
# First the row is positioned as needed with the correct number of spaces
spaces = " " * input_length
for j in range(userinput - i):
print(spaces, end = " ")
for j in range(i, 0, -1):
# Now, the current numbers length is compared to the
# largest number's length, and the appropriate number
# of spaces are appended after the number.
spaces = " " * (input_length + 1 - len(str(j)))
print(j, end = spaces)
for j in range(2,i + 1):
# The same is done here as in the previous loop.
spaces = " " * (input_length + 1 - len(str(j)))
print(j, end = spaces)
print()
j += 1
Take a look at
https://stackoverflow.com/a/13077777/6510412
I think this might be what you're looking for. I hope it helps.

Taking averages of data based on logical filter

we have two columns ('A' and 'B') as follows.
A = [10 5 6 6 10 2 3 2 1 3 2 3 3 7 9 8 6 8 8 12]
B = [10 5 6 6 2 2 3 2 1 3 2 3 3 7 2 2 3 3 8 12]
logicalFilter= ~(B<=3 & B>1)
Now I need to take averages of data points in A corresponding to logicalFilter == 1 for three different blocks of logicalFilter == 1 separately and also ignoring first two points (for example) in A when logicalFilter == 1 in each block for the calculation of averages. How this can be done?
My mentalist skills leading me to this answer:
%// input
A = [10 5 6 6 10 2 3 2 1 3 2 3 3 7 9 8 6 8 8 12]
B = [10 5 6 6 2 2 3 2 1 3 2 3 3 7 2 2 3 3 8 12]
mask = (B<=3 & B>1)
%// get subs and vals for accumarray
C = cumsum(~mask) + 1
[~,~,subs] = unique(C(mask))
val = A(mask)
%// calculate mean starting with 3rd value of group
out = accumarray(subs(:),val(:),[],#(x) mean(x(3:end)) )
out =
2.5000 3.0000 7.0000

matlab get neighbours on matrix

I have a simple matrix:
1 2 3 4
5 6 7 8
8 9 10 11
12 13 14 15
I need to loop through each element and build a new matrix with 3 of its surrounding elements (the one to the right, bottom right and bottom). So I will end up with an array like so:
1 2 6 5
2 3 7 6
3 4 8 7
I managed to do this but when I need to jump to the row below I can't seem to figure out how to do it. for the next row it should be:
5 6 9 8
6 7 10 9
...
Any ideas?
[m n] = size(A);
[jj ii] = ndgrid(1:m-1, 1:n-1); %// rows and columns except last ones
kk = sub2ind([m n], ii(:),jj(:)); %// to linear index
B = [ A(kk) A(kk+m) A(kk+m+1) A(kk+1) ] %// pick desired values with linear index
In your example:
B =
1 2 6 5
2 3 7 6
3 4 8 7
5 6 9 8
6 7 10 9
7 8 11 10
8 9 13 12
9 10 14 13
10 11 15 14
My favourite bsxfun being put to work here -
[M,N] = size(A); %// A is Input
ind = bsxfun(#plus,[1:M-1],[(0:N-2).*M]') %//'
out = A(bsxfun(#plus,ind(:),[0 M M+1 1])) %// Desired output
Output using the sample input from question -
out =
1 2 6 5
2 3 7 6
3 4 8 7
5 6 9 8
6 7 10 9
7 8 11 10
8 9 13 12
9 10 14 13
10 11 15 14

How can I create a 3D array by stenciling the columns of a 2D array in MATLAB?

Suppose I have a 2D array called A. I want to create a 3D array called B, whose "pages" are select columns of a stencil moving across A, column-by-column. For example, the first page of B might be the 1st, 3rd, and 5th columns of A. Then the second page would be the 2nd, 4th, and 6th columns of A, etc.
Anyone have an efficient way of doing this is MATLAB?
Thanks!
I am guessing you are looking for this -
%%// Given 2D array
A = randi(10,4,12)
t1 = reshape(A,size(A,1)*2,[]);
t2 = reshape(t1',size(A,2)/2,[],2); %%//'
B = permute(t2,[2 1 3]) %%// Output 3D array
Output -
A =
5 10 3 5 6 8 4 3 8 10 8 7
10 8 3 7 6 10 9 2 7 8 8 5
10 4 7 8 6 4 5 4 1 1 3 7
7 7 6 6 1 10 5 8 9 4 3 3
B(:,:,1) =
5 3 6 4 8 8
10 3 6 9 7 8
10 7 6 5 1 3
7 6 1 5 9 3
B(:,:,2) =
10 5 8 3 10 7
8 7 10 2 8 5
4 8 4 4 1 7
7 6 10 8 4 3
Of course, there is an alternative straight-forward approach for this special case -
B(:,:,1)=A(:,1:2:end);
B(:,:,2)=A(:,2:2:end);

Resources