xlim command changes the axis limit of the figure.
How can i also limit data that is contained by the figure?
Apparently, even though xlim is applied data is still there.
Example: let's say i have a data set of 5000 elements. but only 1500 elements are shown in a figure. when i save this figure, it will still contain data that is not shown in the figure.
The answer may be particularly useful for people working with matlab2tikz.
Yes, the data are still there. To remove data, use something like this:
>> plot(1:10,(1:10).^2); % just an example
>> h = get(gca,'Children');
>> x = get(h,'XData')
x =
1 2 3 4 5 6 7 8 9 10
>> y = get(h,'YData')
y =
1 4 9 16 25 36 49 64 81 100
>> set(h,'XData',x(2:5), 'YData',y(2:5))
>> set(h,'XData',x(2:5), 'YData',y(2:5))
Related
I have two arrays threshold and values.
threshold=[10 22 97]
values=[99 23 77 11 8 10]
I want to output idx such that threshold(idx-1)<values(i)<=threshold(idx). That is for the above example output will be
output=[4 3 3 2 1 1]
The naive code that can produce above output will be
output=ones(1,length(values))*(length(values)+1);
for i=1:length(values)
for j=1:length(threshold)
if(values(i)>threshold(j))
output(i)=j;
end
end
end
Is there a simple way of doing it. I want to avoid loops.
You can use histc command, with a slight adjustment of threshold array
>> threshold=[-inf 10 22 97 inf];
>> values=[99 23 77 11 8 10];
>> [~, output] = histc( values, threshold+.1 )
output =
4 3 3 2 1 1
The modification of threshold is due to "less-than"/"less-than-equal" type of comparison for bin boundary decisions.
No loops often means you'll gain speed by increasing peak memory. Try this:
threshold = [10 22 97];
values = [99 23 77 11 8 10];
%// Do ALL comparisons
A = sum(bsxfun(#gt, values.', threshold));
%// Find the indices and the ones before
R = max(1, [A; A-1]);
%// The array you want
R(:).'
If you run out of memory, just use the loop, but then with a find replacing the inner loop.
Loops aren't all that bad, you know (if you have MATLAB > R2008). In theory, the solution above shouldn't even be faster than a loop with find, but oh well...profiling is key :)
I was wondering if someone could help me come up with a code for a 3D image I'm working on wright now.
I've got a simple 3D matrix:
A(:,:,1) =
0 7 4
0 32 9
4 3 1
A(:,:,2) =
6 0 4
3 4 6
2 3 11
A(:,:,3) =
12 2 4
10 20 6
14 3 2
I would like to find those values that are bigger than a threshold value (for example biger than 7). However I only want those that are exterior elements, that is, not "central" elements (the 32 on the first layer of the matrix shouldn't be marked as a maximum)
(I'm working with a bigger matrix but I guess that once I'm able to do this for the small 3D matrix from above, it won't be difficult to do it for larger ones).
Thank you a lot
Try this:
A = randn(4,4,4); % data. Arbitrary size
th = 1; % threshold
ind = find(A>th);
[x y z] = ind2sub(size(A), ind);
ext = find((x==1)|(x==size(A,1))|(y==1)|(y==size(A,2))|(z==1)|(z==size(A,3)));
ind_solution = ind(ext); % linear index of desired values
solution = A(ind_solution) % desired values
I'm guessing you could extract vectors from those matrices... so it's a matter of getting the external vectors and looping trough their elements.
I think this link will help you extract a vector.
In Matlab, when I want to plot each row of a n x m matrix A as a line, I do
plot(A');
One problem for me is the x-labels which are indices from 1 to number of variables.
I want to change those labels to more meaningful values from, say, a vector B.
So I tried following statement
plot(repmat(B,1,size(A,1)),A');
but the chart looks totally different. I know I can use 'XTickLabel' but it does not work with line plot of matrix, meaning no effect of 'XTickLabel'. Any idea how I can put labels correctly?
You could use something along the lines of:
>>
A = [
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
];
>>
B = [
15 30 45 60
];
>> plot(A')
>> set(gca, 'XTick', 1:numel(B))
>> set(gca, 'XTickLabel', cellstr(num2str(B'))')
This would give you:
You could try this as well
x = 0:0.1:1;
A = [ x.*x ; exp(-x) ]
plot( x, A' )
Hi i'm looking for a way to take a slice of an array from the near the end to near the beginning. I know I could do this in two parts, then add them, but it seems like such a commonly desired operation I thought matlab probably already has it built in but I couldn't find any information in my search.
To clarify I would like to be able to say:
y = 1:10
y(-3:3) or y(8:3)
returns:
8 9 10 1 2 3
Thanks in advance.
there actually is a way to do it (without splitting it up in a concatenation of the negative and positive part of indices): use the modulo operator on your desired range:
>> y = 1:10;
>> y(mod([-3:3]-1,numel(y))+1)
ans =
7 8 9 10 1 2 3
This result consists of 7 numbers (opposing your desired [8 9 10 1 2 3]), which is logical because -3:3 actually spans 7 numbers.
The number 0 would correspond to y(end) with this method, -1 would correspond to y(end-1), etc.
You can try this:
y = 1:10;
n = 3;
y([end-n+1:end 1:n]);
This returns
ans =
8 9 10 1 2 3
I'm trying to monitor the average temperature in a fabrication every hour to ensure quality control. How can I write a script that looks at the temperature inside the plant as a function of time, and outputs the times when the temperature drops below 10 degrees Celsius and when the temperature is above 80 degrees Celsius. My script should say when the temperature is out of the boundary and what the temperature is. I wanna Use the following data:
Temperature = [-15 -5 5 15 24 33 42 51 59 66 73 79 85 90 78]
The first measurement is made at 5am, the last measurement is made at 7pm. I wanna display the time in a 24 hour system instead of a 12 hour system.
It looks like you'll need to loop through the elements of the Temperature vector and find which ones are either below 10 degrees OR above 80 degrees. In a traditional programming language you would use a FOR loop to go through the elements of an array or vector, but generally in MATLAB you want to avoid FOR loops if you can and instead take advantage of MATLAB's vectorization, because it's much faster.
You'll want to look into the FIND function (type 'help find' into the console for more information). But, for example if I had a vector:
A = [0 1 2 1 2 1 1 0];
And used
find(A==0)
The output would be a vector of the indices of A where the element is equal to 0:
[1 8]
I could similarly do:
find(A==1 & A==0)
And I would get
[1 2 4 6 7 8]
This is useful because while traditionally you access the elements of a vector with an index, you can access the elements of a vector in MATLAB with another vector. For example:
>> A = [-10 4 -2 3];
>> ind = [2 3];
>> A(ind)
ans =
4 -2
MATLAB also makes the syntax a bit easier, because you can use the following shortcut instead of explicitly using the FIND function:
>> A = [-10 4 -2 3];
>> A( A<-5 | A>3)
ans =
-10 4
Which would be the same as using the FIND function:
>> A(find(A<-5 | A>3))
ans =
-10 4
I hope this helps. Sorry for the long post. It takes some time to get used to MATLAB's vectorized way of writing code, but once you do get used to it, you'll find it's very useful for computation.
You could crate a 'time' vector like this:
time = 5:1:19;