Maple: three dimensional fit matrix - arrays

I have a CSV file with X values in the first column, and Y values in the first row, with Z values in the middle like so:
** 39 40 41 42
0.004 2.1802 2.1937 2.2144 2.2379
0.25 1.2409 1.2622 1.2859 1.3073
0.5 1.0538 1.02572 1.04857 1.07059
0.75 0.9479 0.96999 0.98699 1.00675
I can import this into maple as a matrix, but for the maple statistics fit command, it requires the X to be in one column, the Y to be in the second column, and the Z to be in the third column like so:
0.004 39 2.1802
0.004 40 2.1937
0.004 41 2.2144
Is there a way to create the second matrix as Maple wants, or is there a call command for Statistics[Fit] that will allow me to insert the first matrix?

Supposing A is your imported matrix, I create a matrix of three columns X, Y, and Z named XYZ thus:
n:= LinearAlgebra:-RowDimension(A):
m:= LinearAlgebra:-ColumnDimension(A):
XYZ:= Matrix([seq(seq([A[i,1],A[1,j],A[i,j]], j= 2..m), i= 2..n)]):

Related

Ramdomly replace each entry by one of its four neighbours

I iterate through a 100x100 array and pick every time four neighbours (one left of the center node, one above, one right and one below), like in the picture below
the red one is the center node and the blue ones are the neighbours. I struggle to find a convenient way in MATLAB to pick randomly one of the neighbours.
The following assumes that
Each entry is replaced by one of its original neighbours, independently of what happens to other entries.
Each neighbour has the same probability of being picked.
Neighbourhood is defined cyclically. Thus, for example, in the first column the "left" neighbour belongs to the last column.
The code builds a cyclically extended matrix for convenience, and then uses linear indexing to (randomly) select the neighbours.
x = [10 20 30 40; 50 60 70 80; 90 100 110 120]; % example data
x_ext = x([end 1:end 1], [end 1:end 1]); % cyclically extended matrix
ind = bsxfun(#plus, (2:size(x,1)+1).', (1:size(x,2))*(size(x,1)+2)); % linear indices
% of the original matrix in the extended matrix
delta = [-1 1 -size(x_ext,1) size(x_ext,1)]; % possible displacements for neighbours,
% as linear indices
r = delta(randi(4, size(x))); % generate random displacements
result = x_ext(ind + r); % pick neighbours in extended matrix
Example:
>> x
x =
10 20 30 40
50 60 70 80
90 100 110 120
>> result
result =
20 30 70 30
90 100 60 120
50 110 70 40

Add Countif to Array Formula (Subtotal) in Excel

I am new to array formulae and have noticed that while SUBTOTAL includes many functions, it does not feature COUNTIF (only COUNT and COUNTA).
I'm trying to figure out how I can integrate a COUNTIF-like feature to my array formula.
I have a matrix, a small subset of which looks like:
A B C D E
48 53 46 64 66
48 66 89
40 38 42 49 44
37 33 35 39 41
Thanks to the help of #Tom Shape in this post, I (he) was able to average the sum of each row in the matrix provided it had complete data (so rows 2 and 4 in the example above would not be included).
Now I would like to count the number of rows with complete data (so rows 2 and 4 would be ignored) which include at least one value above a given threshold (say 45).
In the current example, the result would be 2, since row 1 has 5/5 values > 45, and row 3 has 1 value > 45. Row 5 has values < 45 and rows 2 and 3 have partially or fully missing data, respectively.
I have recently discovered the SUMPRODUCT function and think that perhaps SUMPRODUCT(--(A1:E1 >= 45 could be useful but I'm not sure how to integrate it within Tom Sharpe's elegant code, e.g.,
=AVERAGE(IF(SUBTOTAL(2,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))=COLUMNS(A1:E1),SUBTOTAL(9,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1))),""))
Remember, I am no longer looking for the average: I want to filter rows for whether they have full data, and if they do, I want to count rows with at least 1 entry > 45.
Try the following. Enter as array formula.
=COUNT(IF(SUBTOTAL(4,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))>45,IF(SUBTOTAL(2,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))=COLUMNS(A1:E1),SUBTOTAL(9,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1))))))
Data

Array sorting in matlab

Hi i have a 289x2 array that i want to sort in MatLab. I want to sort the first column into numerical ascending order. However I want to keep the second column entry that is associated with it. Best way to explain is through an example.
x = 76 1
36 2
45 3
Now I want to sort x so that it returns an array that looks like:
x = 36 2
45 3
76 1
So the first column has been sorted into numerical order but has retained its second column value. So far I have tried sort(x,1). This sorts the first column as i want but does not keep the pairing. This returns x as:
x = 36 1
45 2
76 3
Any help would be great. Cheers!!
This is exactly what sortrows does.
x=sortrows(x); % or x=sortrows(x,1);
or if you want to use sort then get the sorted indexes first and then arrange the rows accordingly like this:
[~, idx] = sort(x); %Finding the sorted indexes
x = x(idx(:,1),:) ; %Arranging according to the indexes of the first column
Output for both approaches:
x =
36 2
45 3
76 1

Matlab finding best fit line from scatter plot but exclude some data points

I use Matlab to find the best fit line from a scatter plot, but I need to delete some data points. For example I am trying to find the best fit line of
x = [10 70 15 35 55 20 45 30];
y = [40 160 400 90 500 60 110 800];
Now I need to delete all y points that value is over 300, and of course deleting corresponding x points, and then make a scatter plot and find the best fit line. So how to implement this?
Now I need to delete all y points that value is over 300, and of course deleting corresponding x points,
There is standard Matlab trick - Logical Indexing (see for example in matrix-indexing):
x = [10 70 15 35 55 20 45 30]; y = [40 160 400 90 500 60 110 800];
filter = (y<300);
y1 = y(filter);
x1 = x(filter);
plot(x,y,'+b',x1,y1,'or');
You can use polyfit (Matlab Doc) function for linear fit:
ff=polyfit(x1,y1,1);
plot(x,y,'*b',x1,y1,'or',x1,ff(1)*x1 + ff(2),'-g');
grid on;
The best way is to logically filter the dataset, then plot it.
NOTE: Data should be in column format. If it isn't, rotate like x'.
filter = (y<300);
x = x.*filter;
x = [zeros(length(x),1),x]; % this is to get the b(0) coefficient
y = y.*filter;
b = x\y;
x = x(:,2); % cleaning up column of zeros
plot(x,y,'bo')
hold on
plot([min(x),max(x)],(b(1)+b(2))*[min(x),max(x)])
hold off
axis tight

Align two arrays in matlab

I have two arrays in matlab representing tracked points by two different methods. In each array the first column contains the frame number, and columns two and three are the x, y coordinates. The tracks don't necessarily start or finish at the same frame, but I want to compare the distances between tracks for any common frames.
My input data is something along the lines of:
d1 = d2 =
[ 130 50 20; [ 128 48 17;
131 50 21; 129 52 19;
... ...
195 70 36 ] 180 65 34 ]
I can find intersecting frame numbers using
commonFrames = intersect(d1(:,1), d2(:,1));
but I'm stuck on how to align these arrays (preferably without a for loop)?
I'd looking for an output along the lines of [frameNumber x1 y1 x2 y2] where x1, y1 are values from frame frameNumber of array d1, and x2, y2 are values from frame frameNumber of array d2.
'intersect' function has two additional output values: indices of common values in input arrays.
Your script can be following:
[commonFrames,ia,ib] = intersect(d1(:,1), d2(:,1));
commonData = [commonFrames d1(ia,2:3) d2(ib,2:3)];

Resources