Values of Variables Matrix NumPy - arrays

I'm working on a program that determines if lines intersect. I'm using matrices to do this. I understand all the math concepts, but I'm new to Python and NumPy.
I want to add my slope variables and yint variables to a new matrix. They are all floats. I can't seem to figure out the correct format for entering them. Here's an example:
import numpy as np
x = 2
y = 5
w = 9
z = 12
I understand that if I were to just be entering the raw numbers, it would look something like this:
matr = np.matrix('2 5; 9 12')
My goal, though, is to enter the variable names instead of the ints.

You can do:
M = np.matrix([[x, y], [w, z]])
# or
A = np.array([[x, y], [w, z]])
I included the array as well because I would suggest using arrays instead of of matrices. Though matrices seem like a good idea at first (or at least they did for me), imo you'll avoid a lot of headache by using arrays. Here's a comparison of the two that will help you decide which is right for you.
The only disadvantage of arrays that I can think of is that matrix multiply operations are not as pretty:
# With an array the matrix multiply like this
matrix_product = array.dot(vector)
# With a matrix it look like this
matrix_product = matrix * vector

Can you just format the string like this?:
import numpy as np
x = 2
y = 5
w = 9
z = 12
matr = np.matrix('%s %s; %s %s' % (x, y, w, z))

Related

How to take irfft of multidimensional array in julia?

I am new to julia, and I am trying to take the irfft of B, which is a 3d array of size (n/2, n, n) where B = rfft(A). However, the irfft in julia reqires an additional input d for the size of the transformed real array, and I'm unsure of what to put. I tried n and n/2, but both did not seem to work as expected when I printed the resulting matrix out.
EDIT: I should've lowered my dimensions to check if everything was working, turns out using d = n is ok. Thanks to everyone who answered!
Check out this discussion. Presumably any triple of numbers will do the trick, but may or may not give you what you want.
This should work:
using FFTW
function test(n = 16)
a = rand(n ÷ 2, n, n)
f = rfft(a)
#show irfft(f, n ÷ 2 + 1)
end
test()

Matlab: Extract LxN array from LxMxN array

Suppose x=zeros(L,M,N). For a fixed component, the remaining array is basically a matrix. So I should be able to do something like y = x(:,2,:). Then, I expect y to be a matrix, i.e. an LxN array. But I instead get a Lx1xN array.
How can I obtain a standard matrix from a three-dimensional array, after I fixed one component? I use matlab.
Use permute to rearrange the dimensions after indexing:
x = zeros(2,3,4); % L×M×N
y = permute(x(:,2,:), [1 3 2]); % move 2nd dimension to 3rd
The code sends the second dimension to the end. This transforms the L×1×N array into an L×N×1 array, which is the same as an L×N matrix, because trailing singleton dimensions are ignored; in fact, arrays can be considered to have an infinite number of trailing singleton dimensions. As a check,
>> size(y)
ans =
2 4
A word of caution: some people may be tempted to use the simpler y = squeeze(x(:,2,:)), but that squeezes all (non-trailing) singleton dimensions, not just the second, and so it gives a wrong result for L=1.
You can use reshape:
y = reshape(x(:,2,:), [L N]);

Matlab: creating 3D arrays as a function of 2D arrays

I want to create 3d arrays that are functions of 2d arrays and apply matrix operations on each of the 2D arrays. Right now I am using for loop to create a series of 2d arrays, as in the code below:
for i=1:50
F = [1 0 0; 0 i/10 0; 0 0 1];
B=F*F';
end
Is there a way to do this without the for loop? I tried things such as:
F(2,2) = 0:0.1:5;
and:
f=1:0.1:5;
F=[1 0 0; 0 f 0; 0 0 1];
to create them without the loop, but both give errors of dimension inconsistency.
I also want to perform matrix operations on F in my code, such as
B=F*F';
and want to plot certain components of F as a function of something else. Is it possible to completely eliminate the for loop in such a case?
If I understand what you want correctly, you want 50 2D matrices stacked into a 3D matrix where the middle entry varies from 1/10 to 50/10 = 5 in steps of 1/10. You almost have it correct. What you would need to do is first create a 3D matrix stack, then assign a 3D vector to the middle entry.
Something like this would do:
N = 50;
F = repmat(eye(3,3), [1 1 N]);
F(2,2,:) = (1:N)/10; %// This is 1/10 to 5 in steps of 1/10... or 0.1:0.1:5
First pre-allocate a matrix F that is the identity matrix for all slices, then replace the middle row and middle column of each slice with i/10 for i = 1, 2, ..., 50.
Therefore, to get the ith slice, simply do:
out = F(:,:,i);
Minor Note
I noticed that what you want to do in the end is a matrix multiplication of the 3D matrices. That operation is not defined in MATLAB nor anywhere in a linear algebra context. If you want to multiply each 2D slice independently, you'd be better off using a for loop. Doing this vectorized with native operations isn't supported in this context.
To do it in a loop, you'd do something like this for each slice:
B = zeros(size(F));
for ii = 1 : size(B,3)
B(:,:,ii) = F(:,:,ii)*F(:,:,ii).';
end
... however, examining the properties of your matrix, the only thing that varies is the middle entry. If you perform a matrix multiplication, all of the entries per slice are going to be the same... except for the middle, where the entry is simply itself squared. It doesn't matter if you multiple one slice by the transpose of the other. The transpose of the identity is still the identity.
If your matrices are going to be like this, you can just perform an element-wise multiplication with itself:
B = F.*F;
This will not work if F is anything else but what you have above.
Creating the matrix would be easy:
N = 50;
S = cell(1,N);
S(:) = {eye(3,3)};
F = cat(3, S{:});
F(2,2,:) = (1:N)/10;
Another (faster) way would be:
N = 50;
F = zeros(3,3,N);
F(1,1,:) = 1;
F(2,2,:) = (1:N)/10;
F(3,3,:) = 1;
You then can get the 3rd matrix (for example) by:
F(:,:,3)

Matlab populate a 3 dimensional array.

I'm quite new to Matlab and am trying to populate a 3 dimensional array. Basically I have 4 lots of 1x81 matrix that I want in one single 4x1x81 matrix. I tried to do this using a for loop to splice each 1x81 into a 4x1x81 but so far haven't had any luck. I'm sure there is a simpler way but need a fresh pair of eyes. Any help would be greatly appreciated, thanks!
I will give you an example and then you apply the same technique. You have to use the colon or : operator to achieve this task.
a=1;b=2;c=3;
Then a 3-D matrix can be formed as:
new3D_Mat(:,:,1)=a;
new3D_Mat(:,:,2)=b;
new3D_Mat(:,:,3)=c;
Output:
>> new3D_Mat
new3D_Mat(:,:,1) =
1
new3D_Mat(:,:,2) =
2
new3D_Mat(:,:,3) =
3
What exactly did you try? How about matrix(1,1,:)=myvector;matrix(2,1,:)=anotherone; or a loop in which you replace the first index with the loop variable? Consider
>> m(1,1,:)=rand(1,3)
m =
(:,:,1) =
0.3478
(:,:,2) =
0.0276
(:,:,3) =
0.5313
Out of curiosity, what is wrong with a 4x81 matrix? (If you already have one, permute may help you get a 4x1x81 3d array.)
Why use a 4x1x81 matrix?
Look at how easy it is to create a 4x81 matrix from four 1x81 matrices.
% Matrix of all ones
a = ones(1, 81);
% Matrix of all twos
b = ones(1, 81);
b = b .*2;
% Matrix of all threes
c = ones(1, 81); c = c .*3;
% Matrix of all fours
d = ones(1, 81); d = d .*4;
% Aggregate
all_of_em = [a; b; c; d];
Run whos to see your variables.
Name Size Bytes Class Attributes
a 1x81 648 double
all_of_em 4x81 2592 double
b 1x81 648 double
c 1x81 648 double
d 1x81 648 double

store data in array from loop in matlab

I want to store data coming from for-loops in an array. How can I do that?
sample output:
for x=1:100
for y=1:100
Diff(x,y) = B(x,y)-C(x,y);
if (Diff(x,y) ~= 0)
% I want to store these values of coordinates in array
% and find x-max,x-min,y-max,y-min
fprintf('(%d,%d)\n',x,y);
end
end
end
Can anybody please tell me how can i do that. Thanks
Marry
So you want lists of the x and y (or row and column) coordinates at which B and C are different. I assume B and C are matrices. First, you should vectorize your code to get rid of the loops, and second, use the find() function:
Diff = B - C; % vectorized, loops over indices automatically
[list_x, list_y] = find(Diff~=0);
% finds the row and column indices at which Diff~=0 is true
Or, even shorter,
[list_x, list_y] = find(B~=C);
Remember that the first index in matlab is the row of the matrix, and the second index is the column; if you tried to visualize your matrices B or C or Diff by using imagesc, say, what you're calling the X coordinate would actually be displayed in the vertical direction, and what you're calling the Y coordinate would be displayed in the horizontal direction. To be a little more clear, you could say instead
[list_rows, list_cols] = find(B~=C);
To then find the maximum and minimum, use
maxrow = max(list_rows);
minrow = min(list_rows);
and likewise for list_cols.
If B(x,y) and C(x,y) are functions that accept matrix input, then instead of the double-for loop you can do
[x,y] = meshgrid(1:100);
Diff = B(x,y)-C(x,y);
mins = min(Diff);
maxs = max(Diff);
min_x = mins(1); min_y = mins(2);
max_x = maxs(1); max_y = maxs(2);
If B and C are just matrices holding data, then you can do
Diff = B-C;
But really, I need more detail before I can answer this completely.
So: are B and C functions, matrices? You want to find min_x, max_x, but in the example you give that's just 1 and 100, respectively, so...what do you mean?

Resources