3D plotting from an m by n matrix - matrix

I have a large file (over 2 million cells) and I need the value of each individual cell to be read as the z coordinate of a 3D plot and the original x,y coordinates of the cell to be the x and y coordinates in the graph. For example:
M =
1 2 3
4 5 6
7 8 9
I need it to be read as (1,1,1) (2,1,2) (3,1,3)
(1,2,4) (2,2,5) (3,2,6)
(1,3,7) (2,3,8) (3,3,9)
And from there I need to create a 3D plot. Please help!

Pseudocode:
size = 3 # set this to the maximum value for x
x = 1
y = 1
z = readvalue()
while read succeeded
plot(x, y, z)
x = x + 1
if x > size
x = 1
y = y + 1
z = readvalue()

Related

Reshaping matrix in N dimensions octave

would appreciate feedback on how to reshape general ND matrix from a file in octave. To set the problem,
if I read data like for 2D case x y z
0 10 13
0 11 -1
0 12 12
1 10 5
1 11 4
1 12 3
2 10 1
2 11 6
2 12 2
reading Z values
matrix = dlmread('data.dat');
z = matrix(:,3);
z = reshape(z, 3, 3).';
x = [0 1 2];
y = [10 11 12];
xi = linspace (min (x), max (x), 200);
yi = linspace (min (y), max (y), 300);
fcn = interpn (x, y, A, xi, yi, "spline");
Now, transposing a matrix doesn't work in ND which here needs to be done. How would one do a ND case pulling from data so to format this for interpn,
thanks, Damir

How to calculate FFT of real even signal of N points with just N/2 points?

Suppose the given signal is x = [0 1 2 3 0 3 2 1]
The FFT of this real even signal is also real even
fft(x) = X = [12.0000 -2.8284 -4.0000 2.8284 -4.0000 2.8284 -4.0000 -2.8284];
Let y = x(1:length(x)/2); i.e. x = [y 0 fliplr(y(2:end)]
I can use fft(y) to calculate X = fft(x)?

Set values of a matrix on positions inside a triangle

I have a N x N matrix will all values equal to zero, then I need to get the coordinates of a triangle and set the values inside this triangle to one (1).
How can I determine the position of each element in the matrix that forms the triangle faces?
Like this 10x10 matrix, I have a triangle set at (9,1),(5,5) and (9,5):
0000000000
0000000000
0000000000
0000000000
0000000000
0000010000
0000110000
0001010000
0010010000
0111110000
I don't need the code made for me, I want to check if there is a proper way (maybe using math) to get the "coordinates".
When you have two points x1,y1 and x2,y2, you can use these to create a formula for the line using "point-slope form"
Calculate slope with m = (y1 - y2) / (x1 - x2)
Then you have a formula of y - y1 = m(x - x1)
This further goes to y = m(x - x1) + y1
So in your example of (9,1),(5,5) you calculate the m = (1 - 5) / (9 - 5) = (-4) / (4) = -1
Then your formula becomes, for that line, y = (-1)(x - 9) + 1
Then iterate between 5 and 9.
f(5) = -(5-9) + 1 = -(-4) + 1 = 4 + 1 = 5
f(6) = -(6-9) + 1 = -(-3) + 1 = 3 + 1 = 4
f(7) = -(7-9) + 1 = -(-2) + 1 = 2 + 1 = 3
f(8) = -(8-9) + 1 = -(-1) + 1 = 1 + 1 = 2
f(9) = -(9-9) + 1 = -(0)) + 1 = 0 + 1 = 1
Triangles have nice properties allowing a very simple algorithm to suffice.
Find Ymax, the topmost Y coordinate set in the triangle. Then for Ymax, find Xmin and Xmax, of the left and rightmost pixels set in that row. Now there are 2 cases. If Xmin == Xmax, then one vertex is (Xmin,Ymax), otherwise two of the coordinates are (Xmin, Ymax) and (Xmax, Ymax).
With this you've found the topmost coordinate or coordinates.
It's pretty simple to continue this reasoning to find the other ones. I'll let you puzzle it out for the fun...
You can combine the min and max-finding in the algorithm above with the algorithm that does the filling as required in the second part of the problem.

issue with sub2ind and matrix of matrix in matlab with images

I here by post the code why I came across while exploring one technique.
Y = repmat((1:m)', [1 n]);
X = repmat(1:n, [m 1]) - labels_left;
X(X<1) = 1;
indices = sub2ind([m,n],Y,X);
final_labels = labels_left;
final_labels(abs(labels_left - labels_right(indices))>=1) = -1;
In above code labels left is single channel image.[m n] is the size of that image. I want to know how this sub2ind works in above code.And Iam also facing problem in the last statement which contains
labels_right(indices)
what the above expression evaluates to.Here labels right is also an image
Maybe a smaller example could help understand:
%# image matrix
M = rand(4,3)
[m n] = size(M)
%# meshgrid, and convert to linear indices
[X,Y] = meshgrid(1:n,1:m)
indices = sub2ind([m,n],Y,X)
%# extract those elements
M(indices)
The matrix M:
>> M
M =
0.95717 0.42176 0.65574
0.48538 0.91574 0.035712
0.80028 0.79221 0.84913
0.14189 0.95949 0.93399
the grid of (x,y) coordinates of all points:
>> X,Y
X =
1 2 3
1 2 3
1 2 3
1 2 3
Y =
1 1 1
2 2 2
3 3 3
4 4 4
converted to linear indices:
>> indices
indices =
1 5 9
2 6 10
3 7 11
4 8 12
then we index into the matrix using those indices.
>> M(indices)
ans =
0.95717 0.42176 0.65574
0.48538 0.91574 0.035712
0.80028 0.79221 0.84913
0.14189 0.95949 0.93399
Note that: M(indices(i,j)) = M(Y(i,j)),X(i,j)).

Matrix linear indexing

I need to traverse a rectangular grid in continuous manner. Here is an example of what I want, the number means sequence:
+ x
y 0 1 2
5 4 3
6 7 8
At each step I know the index in matrix. Is there any way to calculate the coordinates? The inverse mapping for [x + y * width] doesn't help, beacuse it creates "steps" or "jumps". Is there any solution?
Here is explanation for "steps" mentioned above:
+ x
y 0 1 2
3 4 5 //at this moment the X coordinate changes by 3, thus create step
6 7 8
y = index / width
if( y % 2 == 0 )
x = index % width
else
x = width - index % width - 1
I think that should do it. It's a single modification of the standard way of calculating with "steps" as you call them. You are only changing the way the calculation is done based upon the row.
so you need to first increase the "x" component and then decrease right - so that you get a kind of snake-behavior? You will need an if statement (or some kind of modulo - magic). Let my try the magic:
y := floor(i/columnCount)
x = (y mod 2)*(i - y*columCount) + ((y+1) mod 2)*((columnCount -1) - (i - y*columnCount))

Resources