First my question seemed easy, but i still can't find relevant answer to it.
So I have matrix with size of mx4
a = [(2:6)' rand(5,3)]
a =
2. 0.1589990 0.2725595 0.2944531
3. 0.0181815 0.3437655 0.5718074
4. 0.4098371 0.2033702 0.2141770
5. 0.0105835 0.3011945 0.6895462
6. 0.1965310 0.2762596 0.5855573
And vector b with size of mx1. Elements of b always can be find in the first column of a. For example:
b = [2;3;6]
What I want is to select rows whose value in first column matches values of b, i.e.
ans =
2. 0.1589990 0.2725595 0.2944531
3. 0.0181815 0.3437655 0.5718074
6. 0.1965310 0.2762596 0.5855573
All I've come with so far are these solutions:
Ans(1,:) = a(a(:,1)==b(1),:);
Ans(2,:) = a(a(:,1)==b(2),:);
Ans(3,:) = a(a(:,1)==b(3),:);
And slightly automatized version:
Ans = zeros(size(b,1),size(a,2));
for i = 1:size(b,1)
Ans(i,:)=a(a(:,1)==b(i),:)
end
Is there any possibility to make it easy, may be in one line or without loop?
I think ismember is your friend here:
a = [(2:6)' rand(5,3)]
b = [2;3;6]
a(ismember(a(:,1),b),:)
a =
2.000000 0.065121 0.172047 0.490842
3.000000 0.585701 0.650922 0.260139
4.000000 0.266305 0.543419 0.562515
5.000000 0.760789 0.378180 0.560597
6.000000 0.058769 0.926874 0.538872
b =
2
3
6
ans =
2.000000 0.065121 0.172047 0.490842
3.000000 0.585701 0.650922 0.260139
6.000000 0.058769 0.926874 0.538872
Thanks to RTL for noting that a(ismember(a,b),:) might match elements that were not in the first column.
This -
out = a(find(sum(squeeze(any(bsxfun(#eq,a,permute(b,[3 2 1])),2)),2)),:)
Wow, that's a huge thing isn't it!
Slightly shorter -
out = a(any(squeeze(bsxfun(#eq,a(:,1),permute(b,[3 2 1]))),2),:)
And shorterer -
out = a(any(bsxfun(#eq,a(:,1),b'),2),:)
Without bsxfun or ismember? Hmm..try out intersect, that is basically similar to ismember -
[~,ind,~] = intersect(a(:,1),b);
out = a(ind,:)
Related
I have the following codes which I wish to have an output matrix Rpp of (10201,3). I run this code (which takes a bit long) then I check the matrix size of Rpp and I see (1,3), I tried so many things I couldn't find any proper way. The logic of the codes is to take the 6 values (contain 4 constant values and 2 variable values (chosen from 101 values)) and make the calculation for 3 different i1 and store every output vector of 3 in a matrix with (101*101 (pairs of those 2 variable values)) rows and 3 (for each i1) columns.
I appreciate your help
Vp1=linspace(3000,3500,101);
Vp2=3850;
rho1=2390;
rho2=2510;
Vs1=linspace(1250,1750,101);
Vs2=2000;
i1=[10 25 40];
Rpp = zeros(length(Vp1)*length(Vs1),length (i1));
for n=1:length(Vp1)*length(Vs1)
for m=1:length (i1)
for l=1:length(Vp1)
for k=1:length(Vs1)
p=sin(i1)/Vp1(l);
i2=asin(p*Vp2);
j1=asin(p*Vs1(k));
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1(k).^2*p.^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1(k)^2*p.^2;
c=rho1*(1-2*Vs1(k)^2*p.^2)+2*rho2*Vs2^2*p.^2;
d=2*(rho2*Vs2^2-rho1*Vs1(k)^2);
E=b.*cos(i1)./Vp1(l)+c.*cos(i2)/Vp2;
F=b.*cos(j1)./Vs1(k)+c.*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1(l)).*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2).*(cos(j1)/Vs1(k));
D=E.*F+G.*H.*p.^2;
Rpp=((b.*(cos(i1)/Vp1(l))-c.*cos((i2)/Vp2)).*F-(a+d*((cos(i1)/Vp1(l))).*(cos(j2)/Vs2)).*H.*p.^2)./D
end
end
end
end
Try this. You 2 outer loops didn't do anything. You never used m or n so I killed those 2 loops. Also you just kept overwriting Rpp on every loop so your initialization of Rpp didn't do anything. I added an index var to assign the results to the equation to what I think is the correct part of Rpp.
Vp1=linspace(3000,3500,101);
Vp2=3850;
rho1=2390;
rho2=2510;
Vs1=linspace(1250,1750,101);
Vs2=2000;
i1=[10 25 40];
Rpp = zeros(length(Vp1)*length(Vs1),length (i1));
index = 1;
for l=1:length(Vp1)
for k=1:length(Vs1)
p=sin(i1)/Vp1(l);
i2=asin(p*Vp2);
j1=asin(p*Vs1(k));
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1(k).^2*p.^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1(k)^2*p.^2;
c=rho1*(1-2*Vs1(k)^2*p.^2)+2*rho2*Vs2^2*p.^2;
d=2*(rho2*Vs2^2-rho1*Vs1(k)^2);
E=b.*cos(i1)./Vp1(l)+c.*cos(i2)/Vp2;
F=b.*cos(j1)./Vs1(k)+c.*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1(l)).*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2).*(cos(j1)/Vs1(k));
D=E.*F+G.*H.*p.^2;
Rpp(index,:)=((b.*(cos(i1)/Vp1(l))-c.*cos((i2)/Vp2)).*F-(a+d*((cos(i1)/Vp1(l))).*(cos(j2)/Vs2)).*H.*p.^2)./D;
index = index+1;
end
end
Results:
>> size(Rpp)
ans =
10201 3
The way you use the for loop is wrong. You're running the calculation for length(Vp1)*length(Vs1) * length (i1) * length(Vp1) * length(Vs1) times. Here's the correct way. I changed l into lll just so I won't confuse it with the number 1. In each iteration of the first for loop, you're running length(Vs1) times, and you need to assign the result (a 1X3 array) to the Rpp by using a row number specified by k+(lll-1)*length(Vp1).
for lll=1:length(Vp1)
for k=1:length(Vs1)
p=sin(i1)/Vp1(lll);
i2=asin(p*Vp2);
j1=asin(p*Vs1(k));
j2=asin(p*Vs2);
a=rho2*(1-2*Vs2^2*p.^2)-rho1*(1-2*Vs1(k).^2*p.^2);
b=rho2*(1-2*Vs2^2*p.^2)+2*rho1*Vs1(k)^2*p.^2;
c=rho1*(1-2*Vs1(k)^2*p.^2)+2*rho2*Vs2^2*p.^2;
d=2*(rho2*Vs2^2-rho1*Vs1(k)^2);
E=b.*cos(i1)./Vp1(lll)+c.*cos(i2)/Vp2;
F=b.*cos(j1)./Vs1(k)+c.*cos(j2)/Vs2;
G=a-d*(cos(i1)/Vp1(lll)).*(cos(j2)/Vs2);
H=a-d*(cos(i2)/Vp2).*(cos(j1)/Vs1(k));
D=E.*F+G.*H.*p.^2;
Rpp(k+(lll-1)*length(Vp1),:)=((b.*(cos(i1)/Vp1(lll))-c.*cos((i2)/Vp2)).*F-(a+d*((cos(i1)/Vp1(lll))).*(cos(j2)/Vs2)).*H.*p.^2)./D;
end
end
I have the array "A" with values:
101 101
0 0
61.6320000000000 0.725754779522671
73.7000000000000 0.830301150185882
78.2800000000000 0.490917508345341
81.2640000000000 0.602561200211232
82.6880000000000 0.435568593909153
And I wish to remove this first row and retain the shape of the array (2 columns), thus creating the array
0 0
61.6320000000000 0.725754779522671
73.7000000000000 0.830301150185882
78.2800000000000 0.490917508345341
81.2640000000000 0.602561200211232
82.6880000000000 0.435568593909153
I have used A = A(A~=101); , which removes the values as required - however it packs the array down to one column.
The best way is:
A = A(2:end, :)
But you can also do
A(1,:) = []
however it is slightly less efficient (see Deleting matrix elements by = [] vs reassigning matrix)
If you are looking to delete rows that equal a certain number try
A = A(A(:,1)~=101,:)
Use all or any if you want to delete row if either all or any column equals your value:
A = A(all(A~=101,2),:)
I have a struct, that's a <1x1 struct>, and I'm trying to edit a field in the struct based on the values. The field is called GeoDist_Actual and the struct is called GeoDist_str. The field GeoDist_Actual is a <262792x1 double>, and this is the code I was trying to use in order to get rid of the values that are greater than 1.609344e+05.
i =1;
for i=i:size(GeoDist_str.GeoDist_Actual)
if GeoDist_str.GeoDist_Actual(i,1 > 1.609344e+05
GeoDist_str.GeoDist_Acutal(i,1) = [];
end
end
How would I append or alter this code in order to make it function like I'm aiming? I considered setting all the values to 0, but I'm going to have to go backwards from this in order to get back GPS values, doing a reverse-Vincenty(spherical) calculation, and I'd like to just completely get rid of the values that don't comply with the if condition.
If I can narrow down the question at all, let me know, and thank you for your help in advance!
Edit: I've noticed that when I changed out the section
GeoDist_str.GeoDist_Actual(i,1) = [];
for
GeoDist_str.GeoDist_Actual(i,1) = 0;
It didn't actually solve anything, instead it didn't access the field "GeoDist_Actual" within the struct "GeoDist_str", it just created a mirror field with values of 0.
Consider this example:
% a 10-by-1 vector
x = [1;2;3;4;5;6;7;8;9;10];
% remove entries where the value is less than five
x(x<5) = [];
This is called logical indexing, no need for loops.
Consider the following simple example:
A.a = 1:5;
A =
a: [1 2 3 4 5]
now delete all elements bigger 3;
A.a = A.a( ~(A.a > 3) );
A =
a: [1 2 3]
or alternatively:
A.a( A.a > 3 ) = []
For your case it's a little more bulky:
GeoDist_str.GeoDist_Actual = ...
GeoDist_str.GeoDist_Actual( ...
~(GeoDist_str.GeoDist_Actual > 1.609344e+05) )
I have some input such like:
a = -1.60400000000000,-0.604000000000000,0.396000000000000,1.39600000000000,2.39600000000000,3.39600000000000,4.39600000000000,5.39600000000000,6.39600000000000,7.39600000000000
And I want to remove the columns with a value which is negative / 0.
I have tried the following:
a = max(a, 0);
a = a(:,a(:,:)>0);
But to no avail.
Any suggestions also using linear indexing?
Thanks.
Another way to remove elements (besides reassigning the array) is by assigning [] to the elements you want removed. For a 1D row/column vector
a(a<=0) = []
It's not clear what is needed for a matrix, but say you want to remove the columns for which all values in that column are negative:
a(:,all(a<=0,1)) = []
Or if you want to remove columns with any negative values:
a(:,any(a<=0,1)) = []
In order to treat the first element/row of each column as the indicator for removing the entire column:
a(:,a(1,:)<=0) = []
For example:
>> a=rand(5)-0.5
a =
0.0822 -0.3808 0.0447 0.4937 -0.0954
0.0407 0.4398 0.1473 -0.2813 -0.0516
0.3699 0.1456 0.0439 -0.3942 -0.1342
-0.2352 -0.0205 0.2210 -0.3903 0.2635
-0.1819 0.1393 0.0225 -0.4364 0.1279
>> a(:,a(1,:)<=0) = []
a =
0.0822 0.0447 0.4937
0.0407 0.1473 -0.2813
0.3699 0.0439 -0.3942
-0.2352 0.2210 -0.3903
-0.1819 0.0225 -0.4364
for your example you can just use
a(a>0)
I have a struct, which has 2 fields: time and pose. I have multiple instances of this struct composed in an array, so an example of this is:
poses(1)
-time = 1
-pose = (doesn't Matter)
poses(2)
-time = 2
-pose = (doesn't Matter)
poses(3)
-time = 3
-pose = (doesn't Matter)
...
Now when I print this:
poses.time
I get this:
ans =
1
ans =
2
ans =
3
How can I take that output and put it into a vector?
Use brackets:
timevec=[poses.time];
tricky matlab, I know I know, you'll just have to remember this one if you're working with structs ;)
For cases that the field values are vectors (of same size) and you need the result in a matrix form:
posmat = cell2mat({poses.pose}');
That returns each pose vector in a different row of posmat.