Matlab, looking for the largest value in a rotated square - matlab

Let me explain:
I have a matrix sitting in Matlab that contains values of the height of terrain. I want to now the largest value inside a rectangle. However this rectangle is usually rotated with respect to the orientation of the datapoints in the matrix. To illustrate:
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
This shows a matrix where I would like to extract the data within a rectangle. Imagine a rectangle over the bold faced 10. The bold face 10 is the data I want to examine.
I understand you get a jagged edge when you do stuff like this. I actually want my jagged ' pixelated' outline to lie outside the rectangle i define. The data within the set can not change (ie., be interpolated) and I'm looking for the maximum value.
I've already been close to a solution but it didn't work out. At first it seems to be quite simple so hopefully someone with a fresh pair of eyes can help me out.
regards,
Berend

Select the values you're interested in by indexing, then calculate the maximum of those.
Steve on Image Processing has a number of posts on clever ways to do indexing: http://blogs.mathworks.com/steve/category/indexing/

Related

Histogram rebinning in Matlab 2020a, when I don't have a Matlab histogram object

I have two vectors that describe a histogram: bin values/labels and bin count.
I'd like to import those into a Matlab histogram object so that I can change parameters more easily, such as the number of bins.
Here is a simplification what I have:
Center bin values:
BinValues(1:21) = [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10];
and count values per bin
CountValues(1:21) = [ 16 12 3 21 7 8 2 6 0 3 4 6 1 8 3 25 16 5 7 10 16];
My actual histogram has 800000 bins, and I want to vary the number of bins.
In my simplified example above, it would be something like reducing the bins from 21 to 15 or 21 to 11.
What I have can be graphed as a simple bar graph.
bar(BinValues,CountValues);
But, if I want to experiment with different numbers of bins, I think it would be best to use the histogram function/object in Matlab, but I'm not sure how I do this from what I have.
OK Update:
I tried this:
h=histogram('BinEdges',[-10.5:10.5] , 'BinCounts', CountValues);
Now I have a nice histogram, but if I use fewerbins(h), I get an error saying I can't do that while BinCountsMode is in "manual". If I modify it to be in "auto" mode, then my original histogram disappears.
The brute force solution would be to generate a huge array with repeated values as dictated by binCount values, but that seems like a dumb way to solve this problem.

How do I set random numbers that fall in a range in kdb+?

In Kdb+, how do I use the "roll" function to make the random numbers generated fall within a range that doesn't start with 0? For example what if I wanted the range to be within 2-10 instead of 0-10?
What do I have to add to the code to make it fall into a range instead of the default 0-x? I have tried and looked for every method but can't seem to find one.
You could also just roll from 0-8 then add two. This doesn't require a list to be pre-generated
q)2+5?9
10 2 7 10 7
Assuming you want 2-10 inclusive
// quick and simple method
q)10?2+til 8
6 2 4 3 4 3 4 5 4 7
// or function (x)=num to be dealt, (y) start range, (z) end range
q)f:{x?y+til 1+z-y}
q)f[10;10;20]
12 17 10 11 19 12 11 18 18 11
If you supply a list in the right hand argument then you will get a random value from that list. To roll for a random range from 2-10 you can use til to generate the range:
q)2+til 9
2 3 4 5 6 7 8 9 10
q)1?2+til 9
,6
You can even supply a general list to randomly draw from:
q)3?(`abc;2 3f;10;20;30;"text")
2 3f
`abc
"text"
Simple math function for random number generator is:
(rand() mod (1+max- min)) + min
q) f:{x+rand[0] mod 1+y-x}
q) f[5;10]
q) 7
Update: I failed to notice that you wanted to generate couple of random numbers in the range. You could easily modify above function for that:
q) f:{x+(z?0) mod 1+y-x}
q) f[2;10;4]
q) 6 4 7 2

Evenly distribute bar groups in a bar graph with a non-periodic X-Label in MATLAB

I have a Matrix I like to put in a bar chart. This works, however the x-axis is not periodic, it follows the following numbers:
1 2 3 4 5 6 7 8 9 10 12 14 16 18 20 22 24 26 28 30 35 40 45 50 55 60 70 80 90
These values are stored in the variable Batchsizes, the Matrix is stored in the variable valuable
I use the following code:
figure;
bar(Batchsizes,valuable);
set(gca,'Xtick',Batchsizes(1:length(Batchsizes)));
The following output is generated:
As you can see, the graph is crowded on the left and wide on the right. I would like to have the bar groups evenly distributed over the x-axis so that the graph is evenly spaced while preserving the old x-labels.
Any help is really appreciated.
Remember the answer to your previous question? This is exactly the case when you do want to use XTickLabel (because now the ticks will be positioned at 1:1:29 but you want labels to have the values Batchsizes(1),Batchsizes(2),...). Here's one way to do it:
Batchsizes = [1:9, 10:2:28 30:5:55 60:10:90];
valuable = randi(35,numel(Batchsizes),3);
figure; bar(1:numel(Batchsizes),valuable);
set(gca,'XTick',1:numel(Batchsizes),...
'XTickLabel',cellstr(num2str(Batchsizes.')));
The result is:

How can Dijkstra's algorithm apply to both undirected and directed algorithm in one program?

The graph is represented in the format as below:
MAX 12
NODE 1 1
NODE 2 2
NODE 3 3
NODE 4 4
NODE 5 5
NODE 6 6
NODE 7 7
NODE 9 9
NODE 8 8
NODE 10 10
NODE 11 11
NODE 12 12
EDGE 1 2
EDGE 2 3
EDGE 3 4
EDGE 4 5
EDGE 5 6
EDGE 6 7
EDGE 7 8
EDGE 8 9
EDGE 9 10
EDGE 10 11
EDGE 11 12
EDGE 1 12
EDGE 1 3
EDGE 1 4
EDGE 1 6
EDGE 1 8
EDGE 1 11
EDGE 1 10
EDGE 6 10
EDGE 3 6
EDGE 4 6
EDGE 5 7
EDGE 9 11
I need to use the adjacent list to read in those edges. But if I want to use it as an undirected graph, that is , ignore all the directness of all edges. How could I know the connectivity of each pair of nodes?
For example, the shortest distance between (NODE 2, NODE 8) is 2 (2->1>8) in the undirected graph, but using the Dijkstra's algorithm to this graph gets 4 (2->3->6->7->8). How could I represent the undirected graph while still using the same technique to read in edges?
If you really don't want to change the technique of reading in the edges you'd have to iterate over all the other nodes to see if your node is in their adjacency-list instead of the other way around.
This will increase your running time by quite a bit while not saving you much storage so I'd advise to just change the technique of reading in the edges.

visualizing a distance matrix

Sorry if there's already an answer to this. I can't seem to find it.
I'm working on an application that pulls legislators' voting records on bills, and I'm trying to come up with some interesting ways of visualizing the data. There's one idea in my head right now, but I'm not sure it's mathematically possible to do the visualization I want to in two dimensions.
The data begins like this:
HB1 HB2 HB3
Smith 1 0 1
Hill 1 1 1
Davis 0 1 0
Where 1 = aye, 0 = nay.
The next step I take is to measure the "distance" of each legislator from the other by summing the XORs of their voting records, so that each time one legislator disagrees with another they get a distance "point" with that legislator. That creates a table like this:
Smith Hill Davis
Smith 0 1 3
Hill 1 0 2
Davis 3 2 0
So my idea is to graph each legislator as a point on a plane, and to have the distances between those points reflect the distance rating in the table. I think it presents an interesting opportunity to see if there are clusters of legislators with similar voting patterns, etc.
Now, obviously, this is easy to do with 3 points since you can always draw a triangle with three given lengths for sides. But I can't figure out whether it's mathematically possible to graph lots more (35-70) legislators and still have all the distances right within a 2-dimensional space, or whether you potentially need one additional dimension with each legislator after three.
So, for example, is it possible to preserve all the distances if the data table looks like this?
0 13 6 8 10 14 12 14 12 12
13 0 13 13 13 7 9 11 9 7
6 13 0 12 8 16 14 10 12 14
8 13 12 0 12 10 6 10 10 8
10 13 8 12 0 10 12 12 14 14
14 7 16 10 10 0 10 10 12 8
12 9 14 6 12 10 0 12 8 10
14 11 10 10 12 10 12 0 8 10
12 9 12 10 14 12 8 8 0 10
12 7 14 8 14 8 10 10 10 0
If so, does Octave have a built-in function? or can anyone point me to an algorithm?
Ok, found the answer.
No, it's generally not mathematically possible to do what I wanted to do.
The best approximation is an algorithm called multidimensional scaling. Octave has a built-in function: cmdscale.
Hope others may find this helpful.