MATLAB - Error bars separation distance and height - matlab

I have data arrays w and x; I want to plot error bars y distance apart and z distance above and below the points. Is there a way to do this? I've tried manipulating the errorbar function but can't figure it out.
w [1
3
5
8
9
15
17
34
67
79
90
123
63
23
2
]
x[1
2
3
4
5
6
7
8
9
10
11
12
13
14
15]
plot(x,w)hold on;
errorbar(x,w....not sure what to put after);
I'm trying to plot error bars every 3rd point and with a height of +-5

You could simply draw the error bars yourself
for idx = 1:3:length(w)
plot([x(idx) x(idx)],[w(idx)+5 w(idx)-5]);
end
Alternatively you could give a handle to the errorbar function, but I'm not sure if it allows you to modify this stuff.

By setting the right Properties of the errorbarobject you can get what you need.
Note the LData and UData properties, which are used to specify the height below and above the bars as well as the XData and YData.
clear
clc
close all
w = [1 3 5 8 9 15 17 34 67 79 90 123 63 23 2 ];
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
%// Set location on x axis
loc = 1:3:numel(w);
plot(x,w)
hold on;
hErr = errorbar(loc,w(loc),'rx','LData',5,'UData',5,'XData',loc,'YData',w(loc));
Output:

Related

Fill color inside a shape on 3d plot matlab

I have created a shape in matlab which look like this
.
npts = 14;
z = linspace(-1,1,npts);
omz = sqrt(1-z.^2);
xyz=[2 3 1 0.5 2 5 10 15 20 29 32 34 10 2;1 3 8 12 20 25 27 25 23 26 26 23 5 1;z];
plot3(xyz(1,:),xyz(2,:),xyz(3,:),'ro','LineWidth',2);
box on
hold on
fnplt(cscvn(xyz(:,[1:end 1])),'r',2)
hold off
I need to fill different colors in this shape. How can I do this?
Take the output of fnplt.
change the colormap to gray.
use fill3 to color the patch.
All together:
c = fnplt(cscvn(xyz(:,[1:end 1])),'r',2);
colormap gray
fill3(c(1,:),c(2,:),c(3,:),1:size(c,2))
the result:

stacking rows of 2 matrices on top of each other consecutively

I have two matrices, each with about ten rows. I am trying to stack the rows on top of each other consecutively. For example I have matrices A and B, and I have made a for loop to make it repeat the processes of stacking their rows. The problem now is that when I run the script, it only does it for the first row and doesnt continue on. The code is written below
A = [1 2 3 4 5 6 7 8 9 10];
B= [11 12 13 14 15 16 171 18 19 20];
for i2= 1:10
l= A(i2,1);
p= B(i2,1);
for i4= 1:10
i4 = [l;p] ;
end
end
% so the answer will be
% i4 = [1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20]
Please I need some help
I haven't got matlab to test this but:
new = []
for i= 1:10
l= A(i,:);
p= B(i,:);
for j= 1:10
new(end+1) =[l(j),p(j)] ;
end
end
There is surely a nicer way but this should work.
From:
https://stackoverflow.com/a/17766565/7252268
A = [1 2 3 4 5 6 7 8 9 10];
B= [11 12 13 14 15 16 171 18 19 20];
i4 = zeros(size(A,1), size(A,2)+size(B,2));
i4(:,1:2:end) = A;
i4(:,2:2:end) = B;
Alternatively to the solution of Ekin Inceleme you can use
i4 = reshape([A.' B.'].', 1, 2*size(A,2));
providing that A and B are equally sized. However, this one here is not so easy to read.
You don't need any loops!
just try this:
i4 = [A,B];

How to sample matrix elements in matlab

I have a list of coordinates I would like to sample from a Matrix.
Is there any elegant way to do it?
Ideally, something that looks like:
A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
r = 1:5; % row coordinates
c = 5:-1:1; % column coordinates
A(r,c)
ans =
15 14 13 12 11
Which is equivalent to
for k=1:length(r)
A(r(k), c(k))
end
I am sure someone has asked that, but I couldn't find it anywhere.
Applying #excaza comment I was able to solve this with:
rc_ids = sub2ind(size(A), r,c);
A(rc_ids)

How to show only the existing data points on x axis of bar graph in MATLAB?

I need to simple plot B vs. A as bar plot in MATLAB, but I don't want my x axis showing completely from 1 to 274. I only need to show the existing data point on my x axis, which can be done easily in Excel as in the image below. How can MATLAB do this?
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 20 25 27 29 37 40 42 43 48 73 204 242 274];
B=[30 15 5 9 5 6 3 3 2 1 4 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1];
You need to set both 'XTick' and 'XTickLabel' axes properties:
bar(B);
set(gca,'XTickLabel',A)
set(gca,'XTick',1:numel(A));
xlim([0 numel(A)+1]);
Here is an inelegant, but, nonetheless, working solution to your question:
x = [1,4, 6, 7]; % Your data
uni = unique(x)
yMax = length(find(x == mode(x))) + 1;
c = cell(1, length(uni));
c = strread(num2str(uni),'%s')
hist(1:length(uni));
axis([0 length(uni) 0 yMax])
set(gca, 'XTick', 1:length(uni));
set(gca, 'XTickLabel', c);
Basically, this plots the histogram as if the data were spread from 1 to the number of unique elements. Then, it sets the tick marks at each histogram value. Then, it labels each tick mark with the correct number.

Find closest matching distances for a set of points in a distance matrix in Matlab

I have a matrix of measured angles between M planes
0 52 77 79
52 0 10 14
77 10 0 3
79 14 3 0
I have a list of known angles between planes, which is an N-by-N matrix which I name rho. Here's is a subset of it (it's too large to display):
0 51 68 75 78 81 82
51 0 17 24 28 30 32
68 17 0 7 11 13 15
75 24 7 0 4 6 8
78 28 11 4 0 2 4
81 30 13 6 2 0 2
82 32 15 8 4 2 0
My mission is to find the set of M planes whose angles in rho are nearest to the measured angles.
For example, the measured angles for the planes shown above are relatively close to the known angles between planes 1, 2, 4 and 6.
Put differently, I need to find a set of points in a distance matrix (which uses cosine-related distances) which matches a set of distances I measured. This can also be thought of as matching a pattern to a mold.
In my problem, I have M=5 and N=415.
I really tried to get my head around it but have run out of time. So currently I'm using the simplest method: iterating over every possible combination of 3 planes but this is slow and currently written only for M=3. I then return a list of matching planes sorted by a matching score:
function [scores] = which_zones(rho, angles)
N = size(rho,1);
scores = zeros(N^3, 4);
index = 1;
for i=1:N-2
for j=(i+1):N-1
for k=(j+1):N
found_angles = [rho(i,j) rho(i,k) rho(j,k)];
score = sqrt(sum((found_angles-angles).^2));
scores(index,:)=[score i j k];
index = index + 1;
end
end;
end
scores=scores(1:(index-1),:); % was too lazy to pre-calculate #
scores=sortrows(scores, 1);
end
I have a feeling pdist2 might help but not sure how. I would appreciate any help in figuring this out.
There is http://www.mathworks.nl/help/matlab/ref/dsearchn.html for closest point search, but that requires same dimensionality. I think you have to bruteforce find it anyway because it's just a special problem.
Here's a way to bruteforce iterate over all unique combinations of the second matrix and calculate the score, after that you can find the one with the minimum score.
A=[ 0 52 77 79;
52 0 10 14;
77 10 0 3;
79 14 3 0];
B=[ 0 51 68 75 78 81 82;
51 0 17 24 28 30 32;
68 17 0 7 11 13 15;
75 24 7 0 4 6 8;
78 28 11 4 0 2 4;
81 30 13 6 2 0 2;
82 32 15 8 4 2 0];
M = size(A,1);
N = size(B,1);
% find all unique permutations of `1:M`
idx = nchoosek(1:N,M);
K = size(idx,1); % number of combinations = valid candidates for matching A
score = NaN(K,1);
idx_triu = triu(true(M,M),1);
Atriu = A(idx_triu);
for ii=1:K
partB = B(idx(ii,:),idx(ii,:));
partB_triu = partB(idx_triu);
score = norm(Atriu-partB_triu,2);
end
[~, best_match_idx] = min(score);
best_match = idx(best_match_idx,:);
The solution of your example actually is [1 2 3 4], so the upperleft part of B and not [1 2 4 6].
This would theoretically solve your problem, and I don't know how to make this algorithm any faster. But it will still be slow for large numbers. For example for your case of M=5 and N=415, there are 100 128 170 583 combinations of B which are a possible solution; just generating the selector indices is impossible in 32-bit because you can't address them all.
I think the real optimization here lies in cutting away some of the planes in the NxN matrix in a preceding filtering part.