Edge Coordinates of a Cube - matlab

I have a matrix corresponding to 8 vortex of a cube,
CubeVortex = [3 3 0;
0 3 0;
0 3 3;
3 3 3;
0 0 3;
3 0 3;
3 0 0;
0 0 0];
Now I want to get the coordinates of all the edges divided in 3, like,
As you can see, there will be 12x2 = 24 coordinates.
It would be a little hard to write them.
Is there a way to calculate them from CubeVortex?

One way to do this:
Cube = [
3 3 0;
0 3 0;
0 3 3;
3 3 3;
0 0 3;
3 0 3;
3 0 0;
0 0 0];
% find edges by looking for all combinations of points on cube that
% differ by only one coordinate
sections_per_edge = 3;
weights = ((1:sections_per_edge-1) / sections_per_edge).';
edges = []; % indices into Cube
points = [];
n = size(Cube, 1);
for i = 1:n-1
pointA = Cube(i, :);
for j = i+1:n
pointB = Cube(j, :);
if nnz(pointA - pointB) == 1
edges = [edges; i, j];
% find points along edge as weighted average of point A and B
points = [points; weights * pointA + (1 - weights) * pointB];
end
end
end
% plot corners
plot3(Cube(:,1), Cube(:,2), Cube(:,3), '.r', 'markersize', 20)
hold on
% plot points along edges
plot3(points(:,1), points(:,2), points(:,3), '.b', 'markersize', 20)
% draw edges
line([Cube(edges(:,1), 1), Cube(edges(:,2), 1)].', ...
[Cube(edges(:,1), 2), Cube(edges(:,2), 2)].', ...
[Cube(edges(:,1), 3), Cube(edges(:,2), 3)].', 'color', 'k')
axis([-1,4,-1,4])
Result:
Increasing sections_per_edge to 10, you get

One approach could be this -
n = 3; %// number of IDs
m = 3; %// number of columns
combs = dec2base(0:(n+1)^m-1,n+1,m)-'0' %// form repeated combinations
out = c1(sum(ismember(combs,[1 2]),2)==1,:) %// combinations for intermediate points
You can make this generic for a N-point case and more efficient one, with this -
N = 3;
[x,y,z] = ndgrid(0:N,0:N,0:N)
combs = [z(:) y(:) x(:)]
out = combs(sum(combs~=0 & combs~=N,2)==1,:)
Thus, for your 3-point (0 to 3 that is) case, you would have -
out =
0 0 1
0 0 2
0 1 0
0 1 3
0 2 0
0 2 3
0 3 1
0 3 2
1 0 0
1 0 3
1 3 0
1 3 3
2 0 0
2 0 3
2 3 0
2 3 3
3 0 1
3 0 2
3 1 0
3 1 3
3 2 0
3 2 3
3 3 1
3 3 2

This should do it:
NewVortex=[];
for i=1:3
NewVortex=[CubeVortex*i/3;NewVortex];
end
NewVortex

Related

How to leave only the elements that appear once per specific row x column per page?

I have a 3D array. I need to remove any elements that are in the same row, column position but on the next page (3rd dimension), and only use the first occurrence at that position. So if all pages were to multiply the result would be 0.
Since the 3D array may be of any size, I can't hard code solutions like isMember. I also can't use unique because elements can be the same, just not share the same position.
For example, input:
A(:,:,1) = [ 1 0 2];
A(:,:,2) = [ 1 1 0];
A(:,:,3) = [ 0 1 0];
the desired output is:
A(:,:,1) = [ 1 0 2];
A(:,:,2) = [ 0 1 0];
A(:,:,3) = [ 0 0 0];
How can I accomplish this?
Not the most elegant, but at least it works.
A(:,:,1) = [ 1 0 2 ];
A(:,:,2) = [ 1 1 0 ];
A(:,:,3) = [ 0 1 0 ];
for ii = 1:size(A,1)
for jj = 1:size(A,2)
unique_el = unique(A(ii, jj, :)); % Grab unique elements
for kk = 1:numel(unique_el)
idx = find(A(ii,jj,:) == kk); % Contains indices of unique elements
if numel(idx) > 1 % If an element occurs more than once
A(ii, jj, idx(2:end)) = 0; % Set to 0
end
end
end
end
A
A(:,:,1) =
1 0 2
A(:,:,2) =
0 1 0
A(:,:,3) =
0 0 0
I loop over the first two dimensions of A (rows and columns), find any unique elements which occur on a certain row and column location through the third dimensions (pages). Then set all occurrences of a unique element after the first to 0.
Given a more elaborate 3D matrix this still works:
A(:,:,1) = [1 0 2 0; 2 1 3 0];
A(:,:,2) = [1 1 0 0; 2 2 1 0];
A(:,:,3) = [0 1 1 3; 1 2 2 4];
A(:,:,1) =
1 0 2 0
2 1 3 0
A(:,:,2) =
0 1 0 0
0 2 1 0
A(:,:,3) =
0 0 1 3
1 0 2 4
If you want the first non-zero element and discard any element occurring afterwards, simply get rid of the unique() call:
A(:,:,1) = [1 0 2 0; 2 1 3 0];
A(:,:,2) = [1 1 0 0; 2 2 1 0];
A(:,:,3) = [0 1 1 3; 1 2 2 4];
for ii = 1:size(A,1)
for jj = 1:size(A,2)
idx = find(A(ii,jj,:) ~= 0); % Contains indices of nonzero elements
if numel(idx) > 1 % If more than one element
A(ii, jj, idx(2:end)) = 0; % Set rest to 0
end
end
end
A(:,:,1) =
1 0 2 0
2 1 3 0
A(:,:,2) =
0 1 0 0
0 0 0 0
A(:,:,3) =
0 0 0 3
0 0 0 4
My solution assumes, that, for a given "position", EVERY value after the first occurence of any value is cleared. Some of the MATLAB regulars around here had some discussions on that, from there comes the "extended" example as also used in Adriaan's answer.
I use permute and reshape to rearrange the input, so that we have all "positions" as "page" columns in a 2D array. Then, we can use arrayfun to find the proper indices of the first occurence of a non-zero value (kudos to LuisMendo's answer here). Using this approach again, we find all indices to be set to 0.
Let's have a look at the following code:
A(:,:,1) = [1 0 2 0; 2 1 3 0];
A(:,:,2) = [1 1 0 0; 2 2 1 0];
A(:,:,3) = [0 1 1 3; 1 2 2 4]
[m, n, o] = size(A);
B = reshape(permute(A, [3 1 2]), o, m*n);
idx = arrayfun(#(x) find(B(:, x), 1, 'first'), 1:size(B, 2));
idx = arrayfun(#(x) find(B(idx(x)+1:end, x)) + idx(x) + 3*(x-1), 1:size(B, 2), 'UniformOutput', false);
idx = vertcat(idx{:});
B(idx) = 0;
B = permute(reshape(B, o, m , n), [2, 3, 1])
Definitely, it makes sense to have a look at the intermediate outputs to understand the functioning of my approach. (Of course, some lines can be combined, but I wanted to keep a certain degree of readability.)
And, here's the output:
A =
ans(:,:,1) =
1 0 2 0
2 1 3 0
ans(:,:,2) =
1 1 0 0
2 2 1 0
ans(:,:,3) =
0 1 1 3
1 2 2 4
B =
ans(:,:,1) =
1 0 2 0
2 1 3 0
ans(:,:,2) =
0 1 0 0
0 0 0 0
ans(:,:,3) =
0 0 0 3
0 0 0 4
As you can see, it's identical to Adriaan's second version.
Hope that helps!
A vectorized solution. You can use the second output of max to find the index of the first occurence of a nonzero value along the third dimension and then use sub2ind to convert that to linear index.
A(:,:,1) = [ 1 0 2];
A(:,:,2) = [ 1 1 0];
A(:,:,3) = [ 0 1 0];
[~, mi] =max(logical(A) ,[], 3);
sz=size(A) ;
[x, y] =ndgrid(1:sz(1),1:sz(2));
idx=sub2ind( sz, x,y,mi);
result=zeros(sz) ;
result(idx) =A(idx);

Matlab - Create a bigger matrix out of a smaller one with blank spaces

Let's say I have a 3 x 3 matrix (A), and I want to make it a 5 x 5 matrix (B), but the Matrix A has the following content:
1 2 3
4 5 6
7 8 9
And the resulting bigger matrix B, needs to have the following content:
1 0 2 0 3
0 0 0 0 0
4 0 5 0 6
0 0 0 0 0
7 0 8 0 9
I know this can be done with some "Fors" following a sequence like:
%% We get the dimensions of our matrix.
[xLength, yLength] = size(InMat);
%% We create a matrix of the double size.
NewInMat = zeros(xLength * 2, yLength * 2);
%% We prepare the counters to fill the new matrix.
XLenN = (xLength * 2) -1;
YLenN = (yLength * 2) - 1;
for i = 1 : XLenN
for j = 1 : YLenN
if mod(i, 2) ~= 0
if mod(j, 2) ~= 0
NewInMat(i, j) = InMat(i, j);
else
NewInMat(i,j) = mean([InMat(i, j - 1), InMat(i, j + 2)]);
end
end
end
end
But I would like to know if there is an easier way, or if Matlab has a tool for doing this task. Many thanks in advance!
You can use indexing:
InMat = [...
1 2 3
4 5 6
7 8 9];
s = size(InMat)*2-1;
NewInMat(1:2:s(1), 1:2:s(2)) = InMat;
Here NewInMat is allocated and filled at the same time.

Correspondence label and coordinates' points

How to obtain the coordinates of the first and the last appearances (under column-major ordering) of each label present in a matrix?
Example of a label matrix (where labels are 1 to 4):
L = [
1 1 1 1 0 0 0 0
0 0 0 0 2 2 0 0
0 0 0 0 0 0 2 0
0 0 0 0 0 0 0 0
0 0 0 0 0 3 0 0
0 0 0 0 0 0 3 3
0 0 0 4 0 0 0 0
4 4 4 0 0 0 0 0
];
For the above example L, I would like to obtain a matrix of coordinates like:
M = [
1 1 1
1 4 1
2 5 2
3 7 2
5 6 3
6 8 3
8 1 4
7 4 4 ];
Where the 1st column of M contains horizontal coordinates, the 2nd contains vertical coordinates, and the 3rd column contains the label. There should be 2 rows for each label.
With for-loop you can do it like that:
M=zeros(2*max(L(:)),3);
for k=1:max(L(:))
[r,c]=find(L==k);
s=sortrows([r c],2);
M(k*2-1:k*2,:)=[s(1,:) k; s(end,:) k];
end
M =
1 1 1
1 4 1
2 5 2
3 7 2
5 6 3
6 8 3
8 1 4
7 4 4
Maybe somehow with regionprops options you can do it without the loop...
I just had to try it with accumarray:
R = size(L, 1);
[rowIndex, colIndex, values] = find(L); % Find nonzero values
index = (colIndex-1).*R+rowIndex; % Create a linear index
labels = unique(values); % Find unique values
nLabels = numel(labels);
minmax = zeros(2, nLabels);
minmax(1, :) = accumarray(values, index, [nLabels 1], #min); % Collect minima
minmax(2, :) = accumarray(values, index, [nLabels 1], #max); % Collect maxima
temp = ceil(minmax(:)/R);
M = [minmax(:)-R.*(temp-1) temp repelem(labels, 2, 1)]; % Convert index to subscripts
M =
1 1 1
1 4 1
2 5 2
3 7 2
5 6 3
6 8 3
8 1 4
7 4 4
Here's what I got for timing with Dev-iL's script and Adiel's newest code (Note that the number of labels can't go above 127 due to how Adiel's code uses the uint8 values as indices):
| Adiel | Dev-iL | gnovice
-----------------------+---------+---------+---------
20 labels, 1000x1000 | 0.0753 | 0.0991 | 0.0889
20 labels, 10000x10000 | 12.0010 | 10.2207 | 8.7034
120 labels, 1000x1000 | 0.1924 | 0.3439 | 0.1387
So, for moderate numbers of labels and (relatively) smaller sizes, Adiel's looping solution looks like it does best, with my solution lying between his and Dev-iL's. For larger sizes or greater numbers of labels, my solution starts to take the lead.
If you're looking for a vectorized solution, you can do this:
nTags = max(L(:));
whois = bsxfun(#eq,L,reshape(1:nTags,1,1,[]));
% whois = L == reshape(1:nTags,1,1,[]); % >=R2016b syntax.
[X,Y,Z] = ind2sub(size(whois), find(whois));
tmp = find(diff([0; Z; nTags+1])); tmp = reshape([tmp(1:end-1) tmp(2:end)-1].',[],1);
M = [X(tmp), Y(tmp), repelem(1:nTags,2).'];
Or with extreme variable reuse:
nTags = max(L(:));
Z = bsxfun(#eq,L,reshape(1:nTags,1,1,[]));
[X,Y,Z] = ind2sub(size(Z), find(Z));
Z = find(diff([0; Z; nTags+1]));
Z = reshape([Z(1:end-1) Z(2:end)-1].',[],1);
M = [X(Z), Y(Z), repelem(1:nTags,2).'];
Here's my benchmarking code:
function varargout = b42973322(isGPU,nLabels,lMat)
if nargin < 3
lMat = 1000;
end
if nargin < 2
nLabels = 20; % if nLabels > intmax('uint8'), Change the type of L to some other uint.
end
if nargin < 1
isGPU = false;
end
%% Create L:
if isGPU
L = sort(gpuArray.randi(nLabels,lMat,lMat,'uint8'),2);
else
L = sort(randi(nLabels,lMat,lMat,'uint8'),2);
end
%% Equality test:
M{3} = DeviL2(L);
M{2} = DeviL1(L);
M{1} = Adiel(L);
assert(isequal(M{1},M{2},M{3}));
%% Timing:
% t(3) = timeit(#()DeviL2(L)); % This is always slower, so it's irrelevant.
t(2) = timeit(#()DeviL1(L));
t(1) = timeit(#()Adiel(L));
%% Output / Print
if nargout == 0
disp(t);
else
varargout{1} = t;
end
end
function M = Adiel(L)
M=[];
for k=1:max(L(:))
[r,c]=find(L==k);
s=sortrows([r c],2);
M=[M;s(1,:) k; s(end,:) k];
end
end
function M = DeviL1(L)
nTags = max(L(:));
whois = L == reshape(1:nTags,1,1,[]); % >=R2016b syntax.
[X,Y,Z] = ind2sub(size(whois), find(whois));
tmp = find(diff([0; Z; nTags+1])); tmp = reshape([tmp(1:end-1) tmp(2:end)-1].',[],1);
M = [X(tmp), Y(tmp), repelem(1:nTags,2).'];
end
function M = DeviL2(L)
nTags = max(L(:));
Z = L == reshape(1:nTags,1,1,[]);
[X,Y,Z] = ind2sub(size(Z), find(Z));
Z = find(diff([0; Z; nTags+1]));
Z = reshape([Z(1:end-1) Z(2:end)-1].',[],1);
M = [X(Z), Y(Z), repelem(1:nTags,2).'];
end
You can retrive the uniqe values (your labels) of the matrix with unique.
Having them retrived you can use find to get their indices.
Put together your matrix with it.

Find indices of neighbors in masked region

I have this piece of code:
[I, J] = find(mask == 1);
for k = 1 : numel(I)
i = I(k);
j = J(k);
neighbor_ind = [i, j - 1;
i, j + 1;
i - 1, j;
i + 1, j];
end
Now I would like to find all indices s such that [I(s), J(s)] is equal to one of the rows in neighbor_ind. The neighbor indices for which this is not possible should be ignored.
How can I achieve this?
EDIT: Here is a small example.
Suppose we have the mask
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
(it does not have to be rectangular)
[I, J] = find(mask == 1) will give I = [2, 3, 2, 3] and J = [2, 2, 3, 3].
Now lets set i = 2, j = 2.
There are two neighbors inside the mask, and two outside. I want to know where in I, J I need to go look to find these neighbors. In this example, the solution would be I(3), J(3) for the right neighbor, and I(2), J(2) for the bottom neighbor.
You can use nchoosek to generate indices of neighbors.
mask = [...
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0];
[R , C] = find(mask==1);
n = numel(R);
idx_pix = (n:-1:1).';
idx_neighbors = nchoosek(1:n,n-1);
that results:
idx_pix =
4
3
2
1
idx_neighbors =
1 2 3
1 2 4
1 3 4
2 3 4
So in your example (n == 4) neighbors of 4th pixel are [1 2 3]
in the other words :
neighbors of `idx_pix(1)` are `idx_neighbors(1,:)` : 4 ->> 1 2 3
neighbors of `idx_pix(2)` are `idx_neighbors(2,:)` : 3 ->> 1 2 4
neighbors of `idx_pix(3)` are `idx_neighbors(3,:)` : 2 ->> 1 3 4
neighbors of `idx_pix(4)` are `idx_neighbors(4,:)` : 1 ->> 2 3 4
or
neighbors of R(idx_pix(1)) are R(idx_neighbors(1,:))
...
...
...
note: It is more efficient to use linear indices instead of rows and columns so you can use this signature: IDX = find(mask==1);

Histogram from Spherical Plots in Matlab

I have a data in azimuth and elevation angles with radius 1, e.g., point = [azimuth, elevation, radius]. I would like to divide the sphere surface into 64 regions and get a histogram of the points. For example, in the image below, there are 12 points in the first bin (0 deg <= azimuth < 45 deg,0 deg <= elevation < 45 deg)
I am thinking of using Switch/Case to group the points but that would definitely be very inefficient. Is there any better way?
Edited: I would like to find a way to label the regions (any way will do) and get a histogram, e.g myHist = [... 1 1 ... 10 3 ... ]
I'm not completely sure about it, test it with your data, see how it goes,
r = ones(1,100);
a = 2*pi*rand(1,100);
e = 2*pi*rand(1,100);
[X,Y,Z] = sphere(8) ;
[x y z] = sph2cart(a,e,r);
surf(X,Y,Z);hold on;
plot3(x,y,z,'ko','MarkerFaceColor','k');
axis([-1 1 -1 1 -1 1]);
fields = linspace(0,2*pi,9);
c = {fields,fields};
Hist = hist3(cat(2,a',e'),'Edges',c);
Hist(:,end) = []; Hist(end,:) = [];
Hist =
2 1 0 2 2 2 1 0
0 0 2 1 2 2 3 2
1 2 3 2 0 4 4 2
1 0 0 1 1 2 1 4
0 3 1 1 4 1 3 1
3 3 1 3 4 1 1 0
1 5 2 1 0 0 2 0
0 0 3 1 1 0 2 2
sum(sum(Hist)) = 100;
I think it could be better if you use bar3