Extract data with conditions from matrix in Matlab - matlab

I have a matrix in Matlab where each row looks something like this:
1 3 0.112 5.31275 4.61924 -6.50652
And I want to extract to different matrices according to the value presented in the first column. I know how to do this with for loops but I have too many points to leave this running for whatever time it will take to analyze everything. Is there an easier way to do this?
Here is my code with for loops:
accelerometer = 1;
gyroscope = 0;
a = 1;
g = 1;
for i = 1:size(raw,1)
if raw(i,1) == accelerometer
accelData(a,:) = raw(i,2:6);
a = a+1;
else
if raw(i,1) == gyroscope
gyroData(g,:) = raw(i,2:6);
g = g+1;
end
end
end
Thanks!

I belive something like the code below could be used:
%# Create dummy matrix
raw = [rand(10,1)>=0.5 rand(10,5)];
accelerometer = 1;
gyroscope = 0;
accelData = raw(raw(:,1)==accelerometer,:)
gyroData = raw(raw(:,1)==gyroscope,:)
Example output:
accelData =
1.0000 0.3517 0.0759 0.1622 0.4505 0.1067
1.0000 0.5853 0.5308 0.3112 0.2290 0.0046
1.0000 0.9172 0.9340 0.1656 0.1524 0.8173
1.0000 0.3804 0.0119 0.6892 0.0782 0.2599
gyroData =
0 0.8308 0.0540 0.7943 0.0838 0.9619
0 0.5497 0.7792 0.5285 0.9133 0.7749
0 0.2858 0.1299 0.6020 0.8258 0.8687
0 0.7572 0.5688 0.2630 0.5383 0.0844
0 0.7537 0.4694 0.6541 0.9961 0.3998
0 0.5678 0.3371 0.7482 0.4427 0.8001

Related

Creating multidimensional sequences in MATLAB [duplicate]

My question seems wierd, because I know that we can't use a matrix as input in linspace(x1,x2,n) function. But my question is more something like : I have a vector A=linspace(0,Amax,N), and I want to build a serie of vector B_k or big matrix B_k=linspace(0,A(k),N) but without making a for loop that slows down my whole calculation.
% already defined A
rGC=linspace(0,75e-7,N);
for k=1:N
r=rGC(k);
v=linspace(0,A*r,N);
y=f(r,v);
INT=trapz(v,y);
% The same for 8 more integrals
end
Maybe something using interp1, to interpolate something like:
[0 0 0 ... 0 ]
[A(1) A(2) A(3) ... A(N)]
with N rows .... For instance:
N = 5;
Amax = 15;
A = linspace(0, Amax, N);
x = [0 1];
y = zeros(2, N);
y(2, :) = A;
B = interp1(x, y, linspace(0, 1, N))
Which will give:
B =
0 0 0 0 0
0 0.9375 1.8750 2.8125 3.7500
0 1.8750 3.7500 5.6250 7.5000
0 2.8125 5.6250 8.4375 11.2500
0 3.7500 7.5000 11.2500 15.0000
Not sure it will be any faster than a for loop or that even get the point here :)

Linspace using matrix input matlab

My question seems wierd, because I know that we can't use a matrix as input in linspace(x1,x2,n) function. But my question is more something like : I have a vector A=linspace(0,Amax,N), and I want to build a serie of vector B_k or big matrix B_k=linspace(0,A(k),N) but without making a for loop that slows down my whole calculation.
% already defined A
rGC=linspace(0,75e-7,N);
for k=1:N
r=rGC(k);
v=linspace(0,A*r,N);
y=f(r,v);
INT=trapz(v,y);
% The same for 8 more integrals
end
Maybe something using interp1, to interpolate something like:
[0 0 0 ... 0 ]
[A(1) A(2) A(3) ... A(N)]
with N rows .... For instance:
N = 5;
Amax = 15;
A = linspace(0, Amax, N);
x = [0 1];
y = zeros(2, N);
y(2, :) = A;
B = interp1(x, y, linspace(0, 1, N))
Which will give:
B =
0 0 0 0 0
0 0.9375 1.8750 2.8125 3.7500
0 1.8750 3.7500 5.6250 7.5000
0 2.8125 5.6250 8.4375 11.2500
0 3.7500 7.5000 11.2500 15.0000
Not sure it will be any faster than a for loop or that even get the point here :)

for loops and matrices in matlab

I am trying to link two equations, where I use a for loop calculate the value of k from the range of frequencies (eg. 1-5 Hz) then use each of the k values and substitute the values of k into a 6x6 matrix. Can anyone help show me how to create a matrix for each value of k in Matlab?
1st Equation
for f = 1:5; % Range of Frequencies (Hz)
f;
w = 2.*pi.*f; % Angular Frequency (Hz)
p = 8050;% Density of Mild Steel(kg/m^3)
v = 0.30; % Poissons Ratio of Mild Steel
R = 0.02; % Radius of Pipe (m)
E = 210*10^9; % Youngs Modulus of Mild Steel (pa)
a = (w.^2).*p;
b = (p.*(1-(v.^2)).*(R.^2).*(w.^2)-E);
c = (p.*(R.^2).*(w.^2)-E).*E;
**k(f) = sqrt((a.*b)/c); % k = Wave Number**
end
2nd Equation (6x6 Matrix)
k =
L1=0.1;
L2=0.6;
L3=0.6;
D= [0,0,exp(-k*L1),exp(-k*L2),0,0; exp(-k*L1),1,exp(-k*L1),exp(-k*L2),0,0; -k*exp(-k*L1),k,k*exp(-k*L1),-k*exp(-k*L2),0,0;0,0,exp(-k*(L1+L2)),k,-exp(-k*(L1+L2)),-exp(-k*L3);0,0,-k*exp(-k*(L1+L2)),1,k*exp(-k*(L1+L2)),k*exp(-k*L3);0,0,exp(-k*(L1+L2)),1,0,0]
(Answer modified following the comment)
You can try definig the output array D as a 3 dimensions array.
D=NaN(6,6,length(k))
for i=1:length(k)
D(:,:,i)= [0,0,exp(-k(i)*L1),exp(-k(i)*L2),0,0; exp(-k(i)*L1),1,exp(-k(i)*L1),exp(-k(i)*L2),0,0; -k(i)*exp(-k(i)*L1),k(i),k(i)*exp(-k(i)*L1),-k(i)*exp(-k(i)*L2),0,0;0,0,exp(-k(i)*(L1+L2)),k(i),-exp(-k(i)*(L1+L2)),-exp(-k(i)*L3);0,0,-k(i)*exp(-k(i)*(L1+L2)),1,k(i)*exp(-k(i)*(L1+L2)),k(i)*exp(-k(i)*L3);0,0,exp(-k(i)*(L1+L2)),1,0,0]
end
The D array will be:
D(:,:,1) =
0 0 0.9999 0.9993 0 0
0.9999 1.0000 0.9999 0.9993 0 0
-0.0012 0.0012 0.0012 -0.0012 0 0
0 0 0.9991 0.0012 -0.9991 -0.9993
0 0 -0.0012 1.0000 0.0012 0.0012
0 0 0.9991 1.0000 0 0
D(:,:,2) =
0 0 0.9998 0.9985 0 0
0.9998 1.0000 0.9998 0.9985 0 0
-0.0025 0.0025 0.0025 -0.0025 0 0
0 0 0.9983 0.0025 -0.9983 -0.9985
0 0 -0.0025 1.0000 0.0025 0.0025
0 0 0.9983 1.0000 0 0
D(:,:,3) =
0 0 0.9996 0.9978 0 0
0.9996 1.0000 0.9996 0.9978 0 0
-0.0037 0.0037 0.0037 -0.0037 0 0
0 0 0.9974 0.0037 -0.9974 -0.9978
0 0 -0.0037 1.0000 0.0037 0.0037
0 0 0.9974 1.0000 0 0
D(:,:,4) =
0 0 0.9995 0.9971 0 0
0.9995 1.0000 0.9995 0.9971 0 0
-0.0049 0.0049 0.0049 -0.0049 0 0
0 0 0.9966 0.0049 -0.9966 -0.9971
0 0 -0.0049 1.0000 0.0049 0.0049
0 0 0.9966 1.0000 0 0
D(:,:,5) =
0 0 0.9994 0.9963 0 0
0.9994 1.0000 0.9994 0.9963 0 0
-0.0061 0.0062 0.0061 -0.0061 0 0
0 0 0.9957 0.0062 -0.9957 -0.9963
0 0 -0.0061 1.0000 0.0061 0.0061
0 0 0.9957 1.0000 0 0
Hope this helps.
I will try to answer your question...
Firstly, you don't need the initial for loop. You can calculate all values of wavelength without loops.
f = 1:5; % Range of Frequencies (Hz)
w = 2.*pi.*f; % Angular Frequency (Hz)
p = 8050;% Density of Mild Steel(kg/m^3)
v = 0.30; % Poissons Ratio of Mild Steel
R = 0.02; % Radius of Pipe (m)
E = 210*10^9; % Youngs Modulus of Mild Steel (pa)
a = (w.^2).*p;
b = (p.*(1-(v.^2)).*(R.^2).*(w.^2)-E);
c = (p.*(R.^2).*(w.^2)-E).*E;
k = sqrt((a.*b)./c); % k = Wave Number
L1=0.1;
L2=0.6;
L3=0.6;
Then you can use a for loop to calculate the D matrix and a cell array to store the results:
results = cell(1, length(k));
for i = 1:length(k)
results{i} = [
0,0,exp(-k(i)*L1),exp(-k(i)*L2),0,0;...
exp(-k(i)*L1),1,exp(-k(i)*L1),exp(-k(i)*L2),0,0;...
-k(i)*exp(-k(i)*L1),k(i),k(i)*exp(-k(i)*L1),-k(i)*exp(-k(i)*L2),0,0;...
0,0,exp(-k(i)*(L1+L2)),k(i),-exp(-k(i)*(L1+L2)),-exp(-k(i)*L3);...
0,0,-k(i)*exp(-k(i)*(L1+L2)),1,k(i)*exp(-k(i)*(L1+L2)),k(i)*exp(-k(i)*L3);...
0,0,exp(-k(i)*(L1+L2)),1,0,0
]
end
P.S. Without trying to get on to you, I think that what you just want is to someone solve an excercise for you. If this is not the case, ignore my p.s.

Create vectors associated with each entry of an array and save them in a new matrix

Say i have a matrix like A = [1 2; 3 4], and that i need to create 4, vectors each one associated to one entrance of the matrix, such that the first one goes from -1..1, and second from -2..2, and so forth. Wath i try was
for j=1:2
for k=1:2
W=linspace(-A(j,k),A(j,k),4)
end
end
the problem with that line is that it not save the data.
Also i need that to create a new matrix, such that every row be one of the vectors that i mentioned.
I know that on octave i can do
W=linspace(-A,A,4)
but in MATLAB it doesn't work
If you want 4 values evenly distributed between A(k) and A(k), then you can use an anonymous function in combination with linspace this way:
fun = #(x) linspace(-A(x), A(x), 4)
b = fun(1:numel(A))
b =
-1.00000 -0.33333 0.33333 1.00000
-3.00000 -1.00000 1.00000 3.00000
-2.00000 -0.66667 0.66667 2.00000
-4.00000 -1.33333 1.33333 4.00000
Assuming you want [-1 0 1], [-2 -1 0 1 2] etc, then I suggest using arrayfun like this:
A = [1 2;3 4];
b = arrayfun(#(n) -A(n):A(n), 1:numel(A), 'UniformOutput',0)
b =
{
[1,1] =
-1 0 1
[1,2] =
-3 -2 -1 0 1 2 3
[1,3] =
-2 -1 0 1 2
[1,4] =
-4 -3 -2 -1 0 1 2 3 4
Your approach didn't work because you're overwriting W everytime you loop. The following works:
V = zeros(numel(A),4);
for k=1:numel(A)
W(k,:) = linspace(-A(k),A(k),4);
end
The reason why I only use one index for A is because you may use linear indexing in MATLAB. Remember to allocate memory before you assign values to a matrix inside a loop. "Growing" matrices are very slow.
You can do it like that
W = zeros(4,4);
a = reshape(A, 1, 4);
for i=1:4
W(i,:) = linspace(-a(i), a(i), 4);
end
and you obtain
W =
-1.0000 -0.3333 0.3333 1.0000
-3.0000 -1.0000 1.0000 3.0000
-2.0000 -0.6667 0.6667 2.0000
-4.0000 -1.3333 1.3333 4.0000
If you want to generate a fixed number of values (say 4) for each entry of A, you can achieve it in one line:
>> bsxfun(#times, linspace(-1,1,4), A(:))
ans =
-1.0000 -0.3333 0.3333 1.0000
-3.0000 -1.0000 1.0000 3.0000
-2.0000 -0.6667 0.6667 2.0000
-4.0000 -1.3333 1.3333 4.0000

Is there a way to return many columns with linear indexing in MatLab?

Suppose a 3-D matrix :
>> a = rand(3,4,2)
a(:,:,1) =
0.1067 0.7749 0.0844 0.8001
0.9619 0.8173 0.3998 0.4314
0.0046 0.8687 0.2599 0.9106
a(:,:,2) =
0.1818 0.1361 0.5499 0.6221
0.2638 0.8693 0.1450 0.3510
0.1455 0.5797 0.8530 0.5132
I use linear indexing to have many element at a time:
>> index1 = [1 ; 2 ; 1 ; 3];
>> index2 = [1 ; 4 ; 2 ; 3];
>> index3 = [1 ; 1 ; 2 ; 1];
>> indices = sub2ind(size(a), index1, index2, index3)
>> a(indices)
ans =
0.1067
0.4314
0.1361
0.2599
I would like to do the same thing, put return all the values of the first dimensions. The size of this dimension may vary. The return should be, in that case:
>> indices = sub2ind(size(a), ??????, index2, index3);
>> a(indices)
ans =
0.1067 0.9619 0.0046 % a(:,1,1)
0.8001 0.4314 0.9106 % a(:,4,1)
0.1361 0.8693 0.5797 % a(:,2,2)
0.0844 0.3998 0.2599 % a(:,3,1)
Any way to do that in MatLab?
ind1 = repmat((1:size(a,1)),length(index2),1);
ind2 = repmat(index2,1,size(a,1));
ind3 = repmat(index3,1,size(a,1));
indices = sub2ind(size(a),ind1,ind2,ind3)
indices =
1 2 3
10 11 12
16 17 18
7 8 9
a(indices)
ans =
0.1067 0.9619 0.0046
0.8001 0.4314 0.9106
0.1361 0.8693 0.5797
0.0844 0.3998 0.2599
You can get the result you want by doing linear indexing on the last two dimensions separate from the first two dimensions. Even in the 3d data block where you expect to reference by a(:,:,:) you can reference by a(:) (as you know) or a(:,:). The following code finds the sub2ind for the last two dimensions then just repeats them using meshgrid. This ends up being very similar to the solution proposed by #tmpearce but explicitly shows the semi-linear indexing and uses meshgrid instead of repmat:
dim1 = 3;
dim2 = 4;
dim3 = 2;
rand('seed', 1982);
a = round(rand(dim1,dim2,dim3)*10)
% index1 = :
index2 = [1 ; 4 ; 2 ; 3];
index3 = [1 ; 1 ; 2 ; 1];
indices = sub2ind([dim2 dim3], index2, index3)
a(:, indices) % this is a valid answer
[X,Y] = meshgrid(1:dim1, indices)
indices2 = sub2ind([dim1, dim2*dim3], X,Y);
a(indices2) % this is also a valid answer, with full linear indexing