I would like find a matrix from a row and column vectors

2 views (last 30 days)
Hello, I have a column vector xxb of 201 elements and a row vector rr of 501 elements and I would calculate c =xxb^2+rr^2-R^2 with R=0.05. I have to obtain a matrix of (501x201)size.But i found (501x501). Here is below my code. Anyone has a idea. Thank you in advance, Adam
xb=-5:0.05:5;
xxb=xb';% column vector
r=0:0.01:5;
rr=r;% Row vector
R= 0.05;
for l=1:length(xxb);
for m=1:length(rr);
a(l)=2.*xxb(l);
c(l,m)=xxb(l).^2+rr(m).^2+R^2;
end
end

Accepted Answer

Honglei Chen
Honglei Chen on 13 Aug 2012
Edited: Honglei Chen on 13 Aug 2012
If you want 501x201, then you may want to make rr a column and xxb a row
rr = rr(:);
xxb = xxb(:).';
c = bsxfun(@plus,rr.^2,xxb.^2)+R^2;
or with your current configuration
c = (bxsfun(@plus,xxb.^2,rr.^2)+R^2).';

More Answers (1)

adam
adam on 14 Aug 2012
Hello Honglei,
Thank you for your answer. I managed finally to obtain my matrix using for loop. The aim of this code is to find the area of the intersection of two cercles that must to be matrix in order to use it in another code.But I can't find the good result. Here below my code. May be have you an idea. thank you very much in advance.
if true
xb=-5:0.05:5;
xxb=xb';
dr=0.01;
r=0:dr:5;
rr=r;% colonne ligne
ABDN1=ABDN;% colonne ligne
w2=2.25;
R= 0.05;
for l=1:length(xxb);
for m=1:length(rr);
% paramètres a,c,xp,yp
a(l)=2.*xxb(l);
c(l,m)=xxb(l).^2+rr(m).^2-R^2;
if (a(l)>0) &(a(l)<0)
xp(l,m)=c(l,m)./a(l);
yp(l,m)=R^2-((((2.*c(l,m))-(a(l).^2))./(2.*a(l))).^2);
else
xp(l,m)=0;
yp(l,m)=0;
end
% angle theta
theta(l,m)=atan((sqrt(yp(l,m)))./xp(l,m));
if R<rr(m) inter(l,m)=0;
elseif xxb(l)==0 inter(l,m)=2*pi;
elseif xxb(l)<abs(R-r(m)) inter(l,m)=2.*pi; elseif xxb(l)>r(m)+R inter(l,m)=0;
else
inter(l,m)=(atan((sqrt(yp(l,m)))./xp(l,m)));
% part1(l,m)= (r(m).^2).*acos((xxb(l).^2 + r(m).^2 - R^2)/(2.*xxb(l).*r(m))); % part2(l,m) = (R^2).*acos((xxb(l).^2+ R ^2- r(m).^2)/(2.*xxb(l).*R)); % part3(l,m) = 0.5.*sqrt((-xxb(l)+r(m)+R)*(xxb(l)+r(m)-R)*(xxb(l)-r(m)+R)*(xxb(l)+r(m)+R)); % % inter(l,m) = part1(l,m) + part2 (l,m)- part3(l,m);
end
end
end
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!