Octave barh with colors based on third value - bar-chart

A is a 16x124 matrix with the values that plot in the bar chart. I guess the default is to have the colors associated with the index (1 to 124). The trouble I'm having is that I want each section of the bar chart to be color coded based on the corresponding value within matrix B. If B(1,1) = 3, then the box for A(1,1) is green, and so forth.
A = {3,5,6,8,1,3,6,3;
6,3,8,2,3,7,8,4;
2,7,8,3,8,2,1,3;
4,9,6,4,3,6,1,2
};
B = {3,2,3,2,1,0,1,3;
3,1,2,1,2,0,3,1;
1,3,2,3,2,3,1,3;
3,1,2,3,2,3,2,3
};
P=[2016,2017,2018,2019];
For i = 1:4
For j = 1:8
If B(i,j) = 3
C(i,j) = 'g'
Elseif B(i,j) = 2
C(i,j) = 'y'
Elseif B(i,j) = 1
C(i,j) = 'r'
Elseif B(i,j) = 0
C(i,j) = 'k'
End
End
End
H = barh(Positions,A,'stacked');
H.facecolor = C
EDIT:
Tasos, thank you! I got it all worked out. Took me a bit to understand the proper syntax for changing this line:
facevertexcdata(m, :) = Colormap(B(m,n),:);
I also ended up using Prism so that the green to red is intuitive with the intent of the figure.
Final Version of figure

Here's an example of what I think you're trying to do
M = 4;
N = 8;
A = randi( 100, M, N );
P = [ 10, 20, 35, 60 ];
H = barh( P, A, 0.9, 'stacked' );
% define colours
B = { [0, 0, 0], [0, 0, 0.1], [0, 0, 0.2], [0, 0, 0.3], [0, 0, 0.4], [0, 0, 0.5], [0, 0, 0.6], [0, 0, 0.7], [0, 0, 0.8] ;
[0, 0, 0], [0.1, 0, 0], [0.2, 0, 0], [0.3, 0, 0], [0.4, 0, 0], [0.5, 0, 0], [0.6, 0, 0], [0.7, 0, 0], [0.8, 0, 0] ;
[0, 0, 0], [0, 0.1, 0], [0, 0.2, 0], [0, 0.3, 0], [0, 0.4, 0], [0, 0.5, 0], [0, 0.6, 0], [0, 0.7, 0], [0, 0.8, 0] ;
[0, 0, 0], [0.1,0,0.1], [0.2,0,0.2], [0.3,0,0.3], [0.4,0,0.4], [0.5,0,0.5], [0.6,0,0.6], [0.7,0,0.7], [0.8,0,0.8]
};
for n = 1 : N
h = get( H, 'children' ){n};
facevertexcdata = zeros(M,3);
for m = 1 : M
facevertexcdata(m, :) = B{m,n};
end
set( h, 'facevertexcdata', facevertexcdata );
end
UPDATE: If you effectively have a matrix B of linear indices (note, no zeros), you can use that to index an array or cell array of predefined colours accordingly (including one generated by a colormap).
E.g. assume
Colormap = { [1,0,0], % red
[0,1,0], % green
[0,0,1], % blue
[1,0,1] } % violet
B = [ 1,2,3,4;
2,3,4,1;
3,4,1,2; ] % ... etc
Then you can use this to generate a cell array that contains the right color triplet in the appropriate position, as:
C(B)
Alternatively, since you only try to access one element at a time, you can define a normal colormap, and use elements of B to obtain the right color from the colormap at the point in the code where you need it (i.e. for the example above, it would be inside the for loop). E.g. if
> Colormap = jet(4)
Colormap =
0 0 1
0 1 1
1 1 0
1 0 0
% B as above
Then, say, for B(2,2) (= 3), you can obtain the third color in the generated colormap, i.e.
> Colormap( B(2,2), : )
ans =
1 1 0

Related

Problems with sub2ind function in Octave

I am new to Octave, so I was reading documentation and I found sub2ind function. I started to test it, but sometimes it works weird or I just don't understand how it must work.
So this is how subscripts must be converted to linear indices. (Example from documentation)
[(1,1), (1,2), (1,3)] [1, 4, 7]
[(2,1), (2,2), (2,3)] ==> [2, 5, 8]
[(3,1), (3,2), (3,3)] [3, 6, 9]
And this is another example from documentation
s1 = [2, 2];
s2 = [1, 3];
ind = sub2ind ([3, 3], s1, s2)
⇒ ind = 2 8
So if we look at the first example the (2, 2) == 5, but second example says [2, 2] == 2.
The (1, 3) has different results too.
Practically It works as the second example shows.
If I try to use this function with only 1 pair it return the same pair
sub2ind([3, 3], [2, 2])
# ans = [2, 2]
In this test I can't see any relation between input and output
sub2ind([3, 3], [2, 2], [3, 3])
# ans = [8, 8]
Function works this strange(maybe not) way only when it gets 1 pair or when one of pairs is pair kind [x, x](two same values).
But otherwise it works fine, so this test returns that it should:
sub2ind([3, 3], [2, 1], [1, 3])
# ans = [2, 7]
Also it works fine when this variant is used sub2ind (dims, i, j).
How does the function works?
You misunderstand the input format.
Change
s1 = [2, 2];
s2 = [1, 3];
ind = sub2ind ([3, 3], s1, s2)
⇒ ind = 2 8
to this:
row = [2, 2]; % x1 and x2
col = [1, 3]; % y1 and y2
ind = sub2ind ([3, 3], row, col)
⇒ ind = 2 8
You have two inputs that you convert to linear indices:
[x1, y1] = [2, 1] = 2 and [x2 y2] = [2, 3] = 8.
This:
sub2ind([3, 3], [2, 2])
# ans = [2, 2]
appears to be equivalent to:
sub2ind([3, 3], [2, 2], [1, 1])
even though it's not in the documentation.

I tried 'interp2' to measure the intensity of a line in octave but I have a method error, any suggestions

imshow(matrix(:,:,1))
%identify axes
[x y] = ginput(2);
% preallocate matrices
cog = zeros(size(matrix,3),1);
%cog
% loop start
for i = 1:size(maytrix,3)
I = matrix(:,:,i);
%n = ceil(norm([diff(x), diff(y)])); % A rough estimation of number of points
test = interp2(I, 2, linspace(x(1), x(2),n), linspace(y(1), y(2),n));
%test = round(test);
cog(i) = sum((1:length(test)).*test')/sum(test);
% loop end
end
scog = (cog - min(cog)) / (max(cog) - min(cog));
Here's a toy example to get you started.
% Create a 100x100x100 3D matrix with a certain pattern
% Smooth pattern:
matrix = [1 : 100] .' * [1 : 100];
matrix = matrix(:) * [1 : 100];
matrix = reshape( matrix, 100, 100, 100 );
% Alternatively, try a random matrix:
%matrix = randn(100,100,100);
Endpoints = randi( [1, 100], [3, 2] ); % Randomly get 2 3D points within matrix
Numpoints = max( abs( diff( Endpoints, 1, 2 ) ) ); % Choose width of widest dimension
% Create a line in 3D space (containing N points) going from one Endpoint to the other.
Linepoints = [ linspace( Endpoints(1, 1), Endpoints(1, 2), Numpoints );
linspace( Endpoints(2, 1), Endpoints(2, 2), Numpoints );
linspace( Endpoints(3, 1), Endpoints(3, 2), Numpoints ); ];
InterpolatedIntensities = interp3( 1:100, 1:100, 1:100, matrix, Linepoints(1, :), Linepoints(2, :), Linepoints(3, :) );
plot( InterpolatedIntensities );

Normalize UInt16 images for Pix2Pix implementation

i'm trying to use this implementation of Pix2Pix algorithm https://github.com/mrzhu-cool/pix2pix-pytorch.
In this implementation they are using small UInt8 (max color value : 255) images so they are normalizing their images like this :
Image.MAX_IMAGE_PIXELS = None
a = Image.open(join(self.a_path, self.image_filenames[index])).convert('RGB')
b = Image.open(join(self.b_path, self.image_filenames[index])).convert('RGB')
a = a.resize((286, 286), Image.BICUBIC)
b = b.resize((286, 286), Image.BICUBIC)
a = transforms.ToTensor()(a)
b = transforms.ToTensor()(b)
w_offset = random.randint(0, max(0, 286 - 256 - 1))
h_offset = random.randint(0, max(0, 286 - 256 - 1))
a = a[:, h_offset:h_offset + 256, w_offset:w_offset + 256]
b = b[:, h_offset:h_offset + 256, w_offset:w_offset + 256]
a = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(a)
b = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(b)
if random.random() < 0.5:
idx = [i for i in range(a.size(2) - 1, -1, -1)]
idx = torch.LongTensor(idx)
a = a.index_select(2, idx)
b = b.index_select(2, idx)
if self.direction == "a2b":
return a, b
else:
return b, a
But in my case, I want to use UInt16 images (max color value : 65535) , so, if I want to normalize UInt16 images I just need to replace 256 by 65536 or do I need to do other things ?

How to get thrust::unique() to work with a vector of thrust::pair

I'm trying to make a unique thrust device vector of thrust::pair<int, int> using thrust::unique(), but the resulting output doesn't seem to be sorted like it should be. Here's the code in question:
struct make_pair : public thrust::binary_function<int, int, thrust::pair<int, int>> {
__host__ __device__
thrust::pair<int, int> operator()(int x, int y) { return thrust::make_pair(x, y); }
};
std::vector<thrust::pair<int, int>> find_borders_launch(int src_width, int src_height, cv::cuda::GpuMat& d_src) {
thrust::device_vector<int> d_focus_result(src_width * src_height * 9);
thrust::device_vector<int> d_target_result(src_width * src_height * 9);
int* d_focus = thrust::raw_pointer_cast(d_focus_result.data());
int* d_target = thrust::raw_pointer_cast(d_target_result.data());
dim3 num_blocks = {uint(src_width + 1), uint(src_height + 1)};
dim3 threads_per_block = {9, 1, 1};
int substep_size = sizeof(int);
find_borders_kernel <<<num_blocks, threads_per_block>>> (src_width, src_height, d_src.data, d_src.step, d_focus, d_target, substep_size);
thrust::device_vector<thrust::pair<int, int>> d_unique(d_focus_result.size());
thrust::transform(d_focus_result.begin(), d_focus_result.end(), d_target_result.begin(), d_unique.begin(), make_pair() ); //seems to do its job just fine
thrust::unique(thrust::device, d_unique.begin(), d_unique.end());
std::vector<thrust::pair<int, int>> output(d_unique.size());
thrust::copy(d_unique.begin(), d_unique.end(), output.begin() );
return output;
}
and the output looks something like:
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 76
-1, -1
0, 76
0, 0
76, 0
-1, -1
76, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
76, 142
-1, -1
76, 142
0, 0
142, 76
-1, -1
142, 76
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
-1, -1
0, 0
...
...
...
and so on for eight million more entries. The documentation says that the first N = num_unique_elements of the array should be all the unique values, but they're clearly not.
My second issue- I want to do something like this to cut off the garbage values at the end of the unique vector:
T* new_end = thrust::unique(thrust::device, d_unique.begin(), d_unique.end());
std::vector<thrust::pair<int, int>> output(int(new_end));
thrust::copy(d_unique.begin(), d_unique.begin() + new_end, output.begin() );
but that code throws an error ('T' is not actually part of the code but stands for many different types I have tried so far). I have a feeling I'm going about both parts of this question in the complete wrong way, and based on the fact that I can't find any questions about it on google, I assume the answer is obvious for reasons I am ignorant of.
solved, thanks to paleonix's comments. I had to sort the array before running unique on it, and the return value of unique wanted to go into thrust::device_vector<thrust::pair<int, int>>::iterator.
here's the changed code if anyone in the future comes across the same problem. I'm sure it's super janky, but it works.
thrust::device_vector<thrust::pair<int, int>> d_unique(d_focus_result.size());
thrust::transform(d_focus_result.begin(), d_focus_result.end(), d_target_result.begin(), d_unique.begin(), make_pair() ); //seems to do its job just fine
thrust::sort(thrust::device, d_unique.begin(), d_unique.end());
thrust::device_vector<thrust::pair<int, int>>::iterator new_end = thrust::unique(thrust::device, d_unique.begin(), d_unique.end());
int new_length = thrust::distance(d_unique.begin(), new_end);
std::vector<thrust::pair<int, int>> output(new_length);
thrust::copy(d_unique.begin(), d_unique.begin() + new_length, output.begin() );
return output;

Code Golf: Movement in 3 Dimensions

Assuming a 3 dimensional irregular matrix where y = 1.5(x) and z = .5(y).
Further assuming an object starts at 0,0,0 and must move positively in at least two dimensions, and must move in all three dimensions (x+1, y+1, z-1 is okay, x+1, y+1, z=z is not). It may move any number of "spaces", but must move the same number in all directions.
The object is allowed to wraparound (x(max +1) = x(0)).
Move said object from its starting position to (0, max(y), .5(max(z))) For z, round up for fractions (end point in 4, 6, 3 matrix becomes 0, 6, 2)
Input is an Integer (X).
Output is the list of moves you would make (extra credit for showing the number of spaces you moved)
Sample Input/Output:
X = 4
Y = 6 //(4 * 1.5)
Z = 3 // 6 / 2
0, 0, 0 //Start
2, 5, 2 // (+2, -2, +2)
1, 2, 2 // (+4, +4, +4)
3, 4, 0 // (+2, +2, -2)
1, 6, 2 // (-2, +2, +2)
3, 3, 3 // (-3, -3, -3)
1, 5, 1 // (-2, +2, -2)
0, 6, 2 // (-1, +1, -1)
7 Moves.
Lua, 68 Characters
The long version below always solves the problem with one move by searching for the first all positive move that will solve problem.
x=...
y,z=x*3/2,x*3/4
a,b,c=0,y,math.ceil(z/2)
x,y,z=x+1,y+1,z+1
for i=1,math.huge do
if (x*i)%y==b and (x*i)%z==c then x=x*i break end
end
print("0,0,0\n0,"..b..","..c.."//+"..x..",+"..x..",+"..x.."\n1 move.")
Output for x = 12:
0,0,0
0,18,5//+455,+455,+455
1 move.
Output for x = 1000:
0,0,0
0,1500,375//+557424868,+557424868,+557424868
1 move.
Seems like the search could be replaced with some simple algebraic equation. But why stop there? Rules are easier to bend in golfing then doing the actual work.
So, assuming that there is always a single 1 move answer, and that I do not have to disclose the "number of spaces you moved", here is the 68 character golfed answer:
x=...print("0,0,0\n0,"..(x*3/2)..","..math.ceil(x*3/8).."\n1 move.")
Mathematica - Not Golfed
Just to see if we can get the ball rolling
... and trying to understand the problem ....
f[x_] := (
(* Init code *)
xmax = x;
ymax = 3 Round[xmax]/2;
zmax = Round[ymax]/2;
xobj = 0;
yobj = ymax;
zobj = Ceiling[zmax/2];
p = Join[Permutations[{1, 1, -1}], {{1, 1, 1}}];
Print["X = ", xmax, "\nY = ", ymax, "\nZ = ", zmax];
(* Loop *)
i = 0;
pos = {0, 0, 0};
k = "Start";
While[
(npos= {Mod[pos[[1]], xmax+1], Mod[pos[[2]], ymax+1], Mod[pos[[3]], zmax+1]})
!= {xobj, yobj, zobj},
i++;
Print[npos, " // ", k];
pos= npos+ (k= RandomInteger[{1,xmax}] p[[RandomInteger[{1, Length[p]}]]]);
];
Print[npos, " // ", k];
Print[i, " Moves"];
);
Invoke with
f[4]
Sample Output
X = 4
Y = 6
Z = 3
{0,0,0} // Start
{3,4,3} // {3,-3,3}
{0,0,2} // {-3,3,3}
{2,3,1} // {-3,3,3}
{0,6,2} // {3,3,-3}
4 Moves
Not sure if I'm following the rules ...