You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
hi everyone , i need help to do this , thank u
1 view (last 30 days)
Show older comments
clear all
clc
A=10*randn(1,8);
B=10*randn(1,8);
EA=A+2
EB=B+2
E=min(EA,EB)
% i need the value of A and B in a row vector that corrispond to the value of E
thank u
Accepted Answer
Adam Danz
on 8 Apr 2019
Edited: Adam Danz
on 8 Apr 2019
The variable 'idx' tells you whether the minimum came from EA (idx=1) or EB (idx=2). Use that to pull out values from A and B as needed.
[E, idx] = min([EA;EB]); %only works if EA and EB are same size
% If you want one row vector of A and one for B
A0 = A(idx==1);
B0 = B(idx==2);
% If you want one row vector that combines A and B
AB = nan(1, length(E));
AB(idx==1) = A(idx==1);
AB(idx==2) = B(idx==2);
13 Comments
mina massoud
on 8 Apr 2019
thank u very much for ur responding
but how can i do it if i want the value of A and B in a row vector and not the index
thank u again
mina massoud
on 8 Apr 2019
sorry i get the variabile AB and that what i want
thank u very much
u r the best
i accept ur answer
thak u
mina massoud
on 9 Apr 2019
%sorry for asking u again , what if A and B is a matrix 2x8 and i need to get out the column %vector of AB that corrispond to the minimum value of E
A=10*randn(2,8);
B=10*randn(2,8);
MA=mean(A,1)
MB=mean(B,1)
E=min(MA,MB)
[E, idx] = min([MA;MB]); %only works if EA and EB are same size
% If you want one row vector that combines A and B
AB = nan(1, E);
AB(idx==1) = A(idx==1 );
AB(idx==2) = B(idx==2);
Adam Danz
on 9 Apr 2019
The min value of E probably won't be represented at all in A or B since E will be from MA or MB which are means of the columns in A or B.
If I've misunderstood the question, maybe it would help to provide an example.
mina massoud
on 9 Apr 2019
that really what i mean that E corrispond to the min value of MA and MB and what i need is the index of MA and MB to get out the corrispondent column vector of A and B
A >>>>> MA >>>>>> min E >>>> from this i need the corrispondat column vector of A
B>>>>>> MB >>>> min E >>>>> from this i need the corrispondat column vector of B
so at the end i need a matrix 2x8 that contain only the the mix of A and B hat corrispond to the minimum value of MA and MB
A=10*randn(2,8); % from this matrix in need only the column vector that corrispond to the min value of MA
B=10*randn(2,8); %from this matrix in need only the column vector that corrispond to the min value of MB
MA=mean(A,1)
MB=mean(B,1)
E=min(MA,MB)
thank u again for your willingness
mina massoud
on 9 Apr 2019
% yes but it doesnt work on my code the last two line , can u fix it to me , thank u adam
clear all
clc
r0=20000; % raggio
fiA=pi/4; fiB=pi/4; fiC=pi/4; % angolo di elevazione
tetaA=(pi/4); tetaB=(pi/4)+(pi); tetaC=(pi/4)+(3*pi/2); % angolo azimutale
%% i range misurati del bersaglio uno e due
b1=[1200 100] ; % la posizione del primo bersaglio
R0TgT1=sqrt(b1(1,1)^2+b1(1,2)^2); % il modulo
%% posizione dei trasmettitori
TXA=[ r0*cos(fiA)*cos(tetaA) , r0*cos(fiA)*sin(tetaA) , r0*sin(fiA)];
TXB=[ r0*cos(fiB)*cos(tetaB) , r0*cos(fiB)*sin(tetaB) , r0*sin(fiB)];
TXC=[ r0*cos(fiC)*cos(tetaC) , r0*cos(fiC)*sin(tetaC) , r0*sin(fiC)];
%% la base line
rbA=sqrt(TXA(1,1)^2+TXA(1,2)^2+TXA(1,3)^2);
rbB=sqrt(TXB(1,1)^2+TXB(1,2)^2+TXB(1,3)^2);
rbC=sqrt(TXC(1,1)^2+TXC(1,2)^2+TXC(1,3)^2);
%% la distanza tra il satellite e il bersaglio uno
TXA_1=sqrt((TXA(1,1)-b1(1,1)).^2+(TXA(1,2)-b1(1,2)).^2+TXA(1,3).^2);
TXB_1=sqrt((TXB(1,1)-b1(1,1)).^2+(TXB(1,2)-b1(1,2)).^2+TXB(1,3).^2);
TXC_1=sqrt((TXC(1,1)-b1(1,1)).^2+(TXC(1,2)-b1(1,2)).^2+TXC(1,3).^2);
%% la distanza tra il satellite e il bersaglio due
%TXA_2=sqrt((TXA(1,1)-1000).^2+(TXA(1,2)-0).^2+TXA(1,3).^2);
%TXB_2=sqrt((TXB(1,1)-1000).^2+(TXB(1,2)-0).^2+TXB(1,3).^2);
%TXC_2=sqrt((TXC(1,1)-1000).^2+(TXC(1,2)-0).^2+TXC(1,3).^2);
%% range misurato del primo bersaglio
rA_1=TXA_1+R0TgT1-rbA;
rB_1=TXB_1+R0TgT1-rbB;
rC_1=TXC_1+R0TgT1-rbC;
%% range misurato del secondo bersaglio
%rA_2=TXA_2+R0TgT2-rbA;
%rB_2=TXB_2+R0TgT2-rbB;
%rC_2=TXB_2+R0TgT2-rbC;
%% adesso vogliamo calcolare la norma del errore che esce fuori dovuta al fatto che sto facendo delle combinazioni sbagliate
k=0;
intx=10; inty=10; D_x=3000; D_y=1000;
x=[10:intx:D_x]; y=[-D_y/2:inty:D_y/2-1];
var_A=15^2; var_B=15^2; var_C=15^2;
for i=1:length(x)
for j=1:length(y)
TgT=[x(i),y(j)];
k=k+1;
TGT(k,:)=TgT;
TXA_2=sqrt((TXA(1,1)-x(i)).^2+(TXA(1,2)-y(j)).^2+TXA(1,3).^2);
TxA_2(i,j)=TXA_2;
TXB_2=sqrt((TXB(1,1)-x(i)).^2+(TXB(1,2)-y(j)).^2+TXB(1,3).^2);
TxB_2(i,j)=TXB_2;
TXC_2=sqrt((TXC(1,1)-x(i)).^2+(TXC(1,2)-y(j)).^2+TXC(1,3).^2);
TxC_2(i,j)=TXC_2;
R0TgT2=sqrt(x(i)^2+y(j)^2);
rA_2=TXA_2+R0TgT2-rbA;
rB_2=TXB_2+R0TgT2-rbB;
rC_2=TXC_2+R0TgT2-rbC;
RA_2(i,j)=(rA_2); % una matrice 300x100
RB_2(i,j)=(rB_2);
RC_2(i,j)=(rC_2);
for n=1:8
Rg=[rA_1 rA_2 ;rB_1 rB_2 ;rC_1 rC_2 ] ;
[X,Y,Z] = ndgrid(1:2 );
Cm = [Z(:),Y(:),X(:)] ;
r = Rg(sub2ind(size(Rg),repmat(1:3,8,1),Cm ));
%sub2ind (MATLAB Functions) The sub2ind command determines the equivalent single index corresponding to a set
%of subscript values. IND = sub2ind(siz,I,J) returns the linear index equivalent to the row and column
%subscripts I and J for a matrix of size siz .
A= [ TXA; TXB ;TXC ] ;% è una matrice 3x2
A(:,3)=[] ;
%b) definizione del vettore costante K
K= 1/2* [ rbA^2-(r(n,1)+rbA)^2 ; rbB^2-(r(n,2)+rbB)^2 ; rbC^2-(r(n,3)+rbC)^2 ] ; % è una matrice 2x1
% definizione del vettore C
% il vettore C è la somma di due vettori costanti ri+Rbi
C = [ r(n,1) ; r(n,2) ;r(n,3) ] + [ rbA ; rbB; rbC ] ; % è una matrice 2x1
% definizione del vettore a , b
invATA_AT= inv(A'*A)*A'; % è una matrice 3*3
%adesso definisco 'a' e 'b'
a= invATA_AT*K;% è una matrice 3*1
b= invATA_AT*C; % è una matrice 3*1
aTa=a'*a; % 1x1
aTb=a'*b; % 1x1
bTb=b'*b; %1x1
Rt_num_n=-aTb + (sqrt((aTb)^2 -(bTb-1)*aTa));
Rt_denum_n= bTb-1;
R0_n(n)=Rt_num_n/Rt_denum_n;% la misura di R0 positivo 1x1
Rn(n)=norm(R0_n(n));
R_N{i,j}=Rn;
Rt_num_p=-aTb - (sqrt((aTb)^2 -(bTb-1)*aTa));
Rt_denum_p= bTb-1;
R0_p(n)=Rt_num_p/Rt_denum_p; % la misura di R0 negativo 1x1
Rp(n)=norm(R0_p(n));
R_P{i,j}=Rp;
KCX_n=K+C*(R0_n); % il calcolo di (K+C||x||) per il valore di R0 positivo che è una matrice 2x1
KCX_p(:,n)=K+C*(R0_p(:,n)); % il calcolo di (K+C||x||) per il valore di R0 negativo che è una matrice 2x1
x_n(:,n)= invATA_AT*KCX_n(:,n); %è una matrice 3x1 che è il calcole della localizzazione del besaglio per il valore di R0 positivo
X_n{i,j}=x_n;
R0n_st(n)=sqrt(x_n(1,n)^2+x_n(2,n)^2);
x_p(:,n)= invATA_AT*KCX_p(:,n);% è una matrice 3x1 che è il calcole della localizzazione del besaglio per il valore di R0 negativo
X_p{i,j}=x_p;
R0p_st(n)=sqrt(x_p(1,n)^2+x_p(2,n)^2);
R_s{i,j}=[Rn ; Rp];
R_S{i,j}=min(Rn,Rp);
Rm{i,j}=zeros(2,8);
[R_S{i,j}, idx{i,j}] = min([R_N{i,j};R_P{i,j}]);
Rm{i,j} = nan(2, length(R_S ));
Rm(:,idx==1) = x_n(:,idx==1 )
Rm(:,idx==2) = x_p(:,idx==2)
end
end
end
mina massoud
on 9 Apr 2019
i tried also in this way but still not working
Rm{i,j}(:,idx==1) = x_n{i,j}(:,idx==1 )
Rm{i,j}(:,idx==2) = x_p{i,j}(:,idx==2)
mina massoud
on 9 Apr 2019
sorry i have to write it in this way
Rm{i,j}(:,idx==1) = X_n{i,j}(:,idx==1 )
Rm{i,j}(:,idx==2) = X_p{i,j}(:,idx==2)
but still not working
Adam Danz
on 9 Apr 2019
This works
A=10*randn(2,8);
B=10*randn(2,8);
MA=mean(A,1);
MB=mean(B,1);
[E, idx] = min([MA;MB]);
AB = nan(1, length(E));
AB(idx==1) = A(idx==1 );
AB(idx==2) = B(idx==2);
I haven't read through your code but if there's a specific part that you're having trouble implementing, share that part of the code and format it the same way I format the code so it's readable.
mina massoud
on 9 Apr 2019
%this is the part of the code that doesn't work
for i=1:300
for j=1:100
A{i,j}=10*randn(2,8);
B{i,j}=10*randn(2,8);
MA{i,j}=mean(A{i,j},1);
MB{i,j}=mean(B{i,j},1);
[E{i,j}, idx{i,j}] = min([MA{i,j};MB{i,j}]);
AB{i,j} = nan(2, length(E{i,j}));
AB{i,j}(idx==1) = A{i,j}(idx==1 );
AB{i,j}(idx==2) = B{i,j}(idx==2);
end
end
% thank you adam
mina massoud
on 9 Apr 2019
% i tried also this , but still not working
for i=1:300
for j=1:100
A{i,j}=10*randn(2,8);
B{i,j}=10*randn(2,8);
MA=mean(A{i,j},1);
MB=mean(B{i,j},1);
[E, idx] = min([MA;MB]);
AB = nan(2, length(E));
AB(idx==1) = A(:,idx==1);
AA{i,j}=AB
AB(idx==2) = B(:,idx==2);
AA{i,j}=AB
end
end
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)