Set x-axis to non-monotonic values and avoid scaling - matlab

I use matlab to plot a graph where instead of having x-axis increase monotonically, I have my own values. eg 5 14 8 9 12 7 etc.I use set (gca,'XTickLabel',num2str(mydata)) which generally works. However, when mydata is more than four or five digits, Matlab scales the graph and thus x-axis values no longer correspond to their intended points. Any ideas on how to prevent this scaling? To clarify, when I make the figure larger, it shows the plot as it should.

The problem is in your num2str() conversion:
mydata = 1:10;
num2str(mydata)
ans =
1 2 3 4 5 6 7 8 9 10
This means, that each tick will be labelled with this long 1 by n char array. The axes will then be resized to fit the labels inside the figure.
A solution is to create one label per row of a char array:
reshape(sprintf('%2d',mydata),2,[])'
ans =
1
2
3
4
5
6
7
8
9
10

Sort of solution is to write set(gca,'xtick',1:myDataVectorLength) before set (gca,'XTickLabel',num2str(mydata))

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.

Vector with specific number of equally spaced values

I am familiar with the command:
(-5:0.1:5)
which creates a vector of values, equally spaced by 0.1, from -5 to 5.
However, is there a way to produce a vector of values, equally spaced from -5 to 5 such that there are, say, 100 values in the vector.
(-5:0.1:5) gives a vector with 101 values, however, is there a way to get a vector of 100 values without manually calculating the step size?
Yes there is. Use function linspace. See documentation here
linspace(1,10,10)
ans =
1 2 3 4 5 6 7 8 9 10
also the question is a duplicate of this question

Matlab: Bar chart x-axis labels missing

I'm using the following code to create a bar chart in matlab.
a = [1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
bar(a)
set(gca,'XTickLabel',{'one','two','three','four','five','six','seven','eight','nine','zero','one','two','three','four','five','six','seven','eight','nine'})
Code works fine except that x-axis labels don't appear on their corresponding position on the x-axis. How do I resolve this issue?
When you set XTickLabel, you are telling Matlab to replace the text where each tick currently is with new text that you provide. If you run only the first two lines, you will see that Matlab by default has put XTicks on 0:2:20. You can resolve the issue by first telling Matlab to put ticks for each individual bar, and then re-labeling these ticks:
a = [1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
bar(a)
set(gca, 'XTick', 1:length(a))
set(gca,'XTickLabel',{'one','two','three','four','five','six','seven','eight','nine','zero','one','two','three','four','five','six','seven','eight','nine'})
You will have a pretty cluttered x-axis at this point...you would want to look into rotating the XTickLabels with the rotateXLabels function from FileExchange or with the built-in functionality on Matlab14b and later.

MATLAB: Step through iterations of a vector

all.
I have a 15 element array = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];.
I was wondering if there was a command such that it would step through iterations of the array without repeating itself. In other words, since there is a chance that randperm() will create the same matrix twice, I want to step through each permutation only once and perform a calculation.
I concede that there are factorial(15) permutations, but for my purposes, these two vectors (and similar) are identical and don't need to be counted twice:
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[15 14 13 12 11 10 9 8 7 6 5 4 3 2 1]
Thus, is there any way to step through this?
Thanks.
I think what you are looking for is perms. randperm returns a single random permutation, you want all the permutations.
So use
my_permuations = perms([1:15]);
If forward-backward is the same as backward-foward then you can use the top half of the list only...
my_permutation_to_use = my_permutations(1:length(my_permutations)/2, :);
You may compare all permutations, but this would require to store all past permutations. Instead a local decision is better. I recommend this simple rule:
A permutation is valid, if the first element is smaller than the last element.
A permutation is redundant, if the first element is larger than the last element.
For small sizes, this could simply be done with this code:
%generate all permutations
x=perms(1:10)
%select only the valid lines, remove all redundant lines
x(x(:,1)<x(:,end),:)
Remains the problem, that generating x for 1:15 breaks all memory limits and would require about 100h.

Permutation vectors from the CLUSTERGRAM object (MATLAB)

I'm using the CLUSTERGRAM object from the Bioinformatics Toolbox (ver 3.7).
MATLAB version R2011a.
I'd like to get permutation vectors for row and columns for clustergram, as I can do with dendrogram function:
x = magic(10);
>> [~,~,permrows] = dendrogram(linkage(x,'average','euc'))
permrows =
9 10 6 7 8 1 2 4 5 3
>> [~,~,permcols] = dendrogram(linkage(x','average','euc'))
permcols =
6 7 8 9 2 1 3 4 5 10
I found that the clustering is not the same from clustergram and dendrogram, most probably due to optimal leaf ordering calculation (I don't want to disable it).
For example, for clustergram from:
clustergram(x)
('average' and 'eucledian' are default methods for clustergram)
the vectors (as on the figure attached) should be:
permrows = [1 2 4 5 3 10 9 6 7 8];
permcols = [1 2 8 9 6 7 10 5 4 3];
So, how to get those vectors programmatically? Anybody well familiar with this object?
Do anyone can suggest a good alternative? I know I can create a similar figure combining imagesc and dendrogram functions, but leaf ordering is much better (optimal) in clustergram, than in dendrogram.
From looking at the documentation, I guess that get(gco,'ColumnLabels') and get(gco,'RowLabels'), where gco is the clustergram object, should give you the reordered labels. Note that the corresponding set-methods take in the labels in original order and internally reorders them.
Consequently, if you have used custom labels (set(gco,'RowLabels',originalLabels))
[~,permrows] = ismember(get(gco,'RowLabels'),originalLabels)
should return the row permutation.