extracting variable from multiple matlab files into one file - matlab

I have this project task, and I'm having problems solving it.
I took samples of the words from 1 to 10 spoken by 10 people.
From each sample I extracted each word e.g, I extracted the word 1 from all samples into different files. I now have 10 files each having the extracted first word. I want to combine these into one single array.
file = wavread( 'G:\Segmented Data\amir.wav');
t = linspace(0,8,length(file));
t2=linspace(0,.8,8820);
section1 = file(1:8820,:);
sound(section1, 11025);
figure(1),
plot(t2,section1);
I have 10 files having the above code. I want to extract the variable section from all these into a new file, and store them in an array.

Do you want to concatenate arrays?
>> a = 1.0:0.1:1.9 % your data, obtained from `wavread()`
a =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000
>> b = 2.0:0.1:2.9 % your data, obtained from `wavread()`
b =
2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000
>> c = 3.0:0.1:3.9 % your data, obtained from `wavread()`
c =
3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000
>> combined = [a; b; c] % a, b, and c in one array
combined =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000
2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000
3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000
This will, of course, only work if a, b and c are the same number of columns wide. If they are different sizes, you will have to pad them out with zeros so that they are the same size.

Related

Save cell with numbers to text file

I have cell arrays A and B with different lengths and numbers.
A={1:0.5:5;1:0.5:2};
B={1:0.5:6;1:0.5:9};
C= [A;B];
I want to combine these cell arrays into a cell array C, which would then look like this:
C =
4×1 cell array
{1×9 double}
{1×3 double}
{1×11 double}
{1×17 double}
Then, I want to save this into a text file, that should look like this:
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000
1.0000 1.5000 2.0000
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000
So far, I have only found code for text or same size arrays. This is my attempt, which doesn't work:
fid = open('filename.txt', 'wt');
fprintf(fid, '%f',C{:})
close(fid)
I believe the problem might be in the format you're specifying for the fprintf, as I believe using only '%f' will print one number on each row.
One way to do this would then be:
fid = fopen('filename.txt', 'wt');
for i = 1:length(C)
fmt = repmat('%f ',size(C{i})); % this only adds one whitespace in between numbers
fmt = [fmt,'\n']; % remember to add a new line
fprintf(fid,fmt,C{i});
end
fclose(fid);

Find the closest value in each group in a matrix column

This is another operation based on the same matrix data, as I talked about in the previous question. I have a matrix as below.
a = [1.05 2.1 3.4 1; 1.06 2.2 3.6 1; 2.04 2.3 3.8 2; 2.15 2.2 4.0 2; 1.37 2.3 3.7 1;3.12 2.1 4.1 3;3.02 2.2 4.2 3;3.42 2.3 4.5 3;3.24 2.4 4.8 3]
a =
1.0500 2.1000 3.4000 1.0000
1.0600 2.2000 3.6000 1.0000
2.0400 2.3000 3.8000 2.0000
2.1500 2.2000 4.0000 2.0000
1.3700 2.3000 3.7000 1.0000
3.1200 2.1000 4.1000 3.0000
3.0200 2.2000 4.2000 3.0000
3.4200 2.3000 4.5000 3.0000
3.2400 2.4000 4.8000 3.0000
a(:,4) is group numbers. Based on group numbers, I split the matrix data into 3 groups: 1, 2 and 3.
I would like to find the index of value closest to 2.2 in a(:,2) in each group. From the data you can see, there is a 2.2 in row 2 belong to group 1, a 2.2 in row 4 belong to group 2, and a 2.2 in row 7 belong to group 3.
My code is shown as below:
[minValue,closestIndex] = splitapply(#(x)min(abs(2.2-x)), a(:,2), findgroups(a(:,4)))
The outcome is:
minValue =
0
0
0
This is consistent as we can find from the data.
closestIndex =
2
2
2
This is supposed to be the indexes of three 2.2 in the matrix, which should be 2, 4 and 7. But the outcome is 2, 2 and 2.
What is wrong with my code?
How to solve this problem?
I think you misunderstand how this function works. It split your initial setup:
a =
1.0500 2.1000 3.4000 1.0000
1.0600 2.2000 3.6000 1.0000
2.0400 2.3000 3.8000 2.0000
2.1500 2.2000 4.0000 2.0000
1.3700 2.3000 3.7000 1.0000
3.1200 2.1000 4.1000 3.0000
3.0200 2.2000 4.2000 3.0000
3.4200 2.3000 4.5000 3.0000
3.2400 2.4000 4.8000 3.0000
to
x1=
1.0500 2.1000 3.4000 1.0000
1.0600 2.2000 3.6000 1.0000 (index = 2)
1.3700 2.3000 3.7000 1.0000
x2=
2.0400 2.3000 3.8000 2.0000
2.1500 2.2000 4.0000 2.0000 (index = 2)
x3=
3.1200 2.1000 4.1000 3.0000
3.0200 2.2000 4.2000 3.0000 (index = 2)
3.4200 2.3000 4.5000 3.0000
3.2400 2.4000 4.8000 3.0000
The indexes are examined separately so indeed - 2,2,2 is correct answer for this query. According to the documentation "The splitapply function calls func once per group" so there is not simple possibility to obtain indexes from initial matrix directly. Maybe you can workaround it by adding another column like 1 2 3 4 5 6 7 8 9 and store the number from it or something in this way but it is not so elegant and would require quite complicated func.

implementing "not equal to " loop in matlab

i have a simple problem i am quite new to matlab so i am having problem in implementing it i have two 64x2 matrices u and h.i have to check if a single row in u is not equal to all of the rows in h.then the row which is not equal should be saved in a separate matrix meanwhile i have written this code but what it does is that r(i,:) get all the values of u(i,:) when this code runs, what i want is that only those values of u(i,:) should be stored in r which are not similar to any row in h matrix.
h=[];
for j=1:8
for i=1:8
h=[h; i j];
end
end
u=[5.3,1.4;6,8;2,3;3,5.5;2.6,8;3.7,2;4,2;5,3;1.9,8;5.4,4;3.2,3;2,2;2,4;2,3;8,2.2;8,4;7.3,1.5;6.2,5.1;2.4,1.5;3,5;2,7.1;1.8,2.7;3,4;6,5;6,1;5,4;4,6;3.5,2;5,7;7.2,8;7,7;5,5;6,3;6,6;1,2;5,8;3,5;1,5;2,2;2,1;6,3;4,7;6,8;3,6;1,6;5,2;3,5;8,7;8,4;4,8;1,1;6,3;7,5;8,1;1,6;4,5;5,5;6,7;6,7;6,7;6,3;3,4;5,7;1,1]
for i=1
for j=1:64
if u(i,:)==h(j,:)
c=1
else
c=0
if c==0
r(i,:)=u(i,:)
end
end
end
end
can anyone help me please
You can do it in one line with ismember:
r = u(~ismember(u,h,'rows'),:);
With your example data, the result is
>> r
r =
5.3000 1.4000
3.0000 5.5000
2.6000 8.0000
3.7000 2.0000
1.9000 8.0000
5.4000 4.0000
3.2000 3.0000
8.0000 2.2000
7.3000 1.5000
6.2000 5.1000
2.4000 1.5000
2.0000 7.1000
1.8000 2.7000
3.5000 2.0000
7.2000 8.0000
use setdiff with 'rows' option to compute r. Please avoid unnecessary loops. pre-allocate when possible.
% construct h without loop
[h{1} h{2}]=ndgrid(1:8,1:8);
h=[h{1}(:) h{2}(:)];
% get r using setdiff
r = setdiff( u, h, 'rows')
Results with
r =
1.8000 2.7000
1.9000 8.0000
2.0000 7.1000
2.4000 1.5000
2.6000 8.0000
3.0000 5.5000
3.2000 3.0000
3.5000 2.0000
3.7000 2.0000
5.3000 1.4000
5.4000 4.0000
6.2000 5.1000
7.2000 8.0000
7.3000 1.5000
8.0000 2.2000
Solution of you question in NlogN complexity (N=64):
N=size(h,1);
[husorted,origin_husorted,destination_hu]=unique([h;u],'rows','first');
iduplicates=destination_hu(N+1:end)<=destination_hu(N),:);
r=u;
r(iduplicates,:)=0;
destination_uh is the only output of unique that is useful; It verifies [h;u]=husorted(destination_uh,:)]. 'first' ensures that if line i of u is equal line j of h, then destination_uh(i+N) is equal to destination_uh(j).
Solution for your particular h, with complexity N:
r=u;
r(all(u==round(u)&u>=1&u<=8,2),:)=0;

How do I append elements to a matrix using a for loop?

I have following data matrix, I want to iterate over this matrix and look at a value in the last column based on a given row and add that row - last element of that row to a new matrix.
5.1000 3.3000 1.7000 0.5000 1.0000
6.8000 3.2000 5.9000 2.3000 3.0000
5.0000 2.3000 3.3000 1.0000 2.0000
7.4000 2.8000 6.1000 1.9000 3.0000
6.5000 3.2000 5.1000 2.0000 3.0000
4.8000 3.4000 1.9000 0.2000 1.0000
4.9000 3.0000 1.4000 0.2000 1.0000
5.1000 3.8000 1.5000 0.3000 1.0000
5.1000 3.4000 1.5000 0.2000 1.0000
5.5000 2.6000 4.4000 1.2000 2.0000
This is the code that I have
M1 = [];
M2 = [];
M3 = [];
for i=1:length(currentCell)
if currentCell(1,5) == 1.00
m3Data = currentCell(1:1,1:4);
%how can I add m3Data to M1
end
end
Let your original matrix be M, then this
M1 = M(find(M(:,5)==1),1:4)
puts all the rows ending with a 1 into M1, excluding the final column. Is that what you want ?
You could do it with a for loop if you want, but I don't see any need.

Matlab: 10 fold cross giving same fold over and over

I have attached the data at the bottom of the question: I have following code that return the 10 fold in a cell, such a way that cell{1,1} consider the training data and cell{1,2} test data but for some reason beyond cell{1,1} which cell{1,2}.... has same data repeating over and over. I am not really sure why its happening. I debug so many rounds and was not able to figure the reason.
Here is the code,
%Function that accept data file as a name and the number of folds
%For the cross fold
function [results_cell] = GetTenFold(dataFile, x)
%loading the data file
dataMatrix = load(dataFile);
%combine the data and labels as one matrix
X = [dataMatrix.data dataMatrix.labels];
%geting the length of the of matrix
dataRowNumber = length(dataMatrix.data);
%shuffle the matrix while keeping rows intact
shuffledMatrix = X(randperm(size(X,1)),:);
crossValidationFolds = x;
%Assinging number of rows per fold
numberOfRowsPerFold = dataRowNumber / crossValidationFolds;
%Assigning 10X2 cell to hold each fold as training and test data
results_cell = cell(10,2);
%starting from the first row and segment it based on folds
i = 1;
for startOfRow = 1:numberOfRowsPerFold:dataRowNumber
testRows = startOfRow:startOfRow+numberOfRowsPerFold-1;
if (startOfRow == 1)
trainRows = (max(testRows)+1:dataRowNumber);
else
trainRows = [1:startOfRow-1 max(testRows)+1:dataRowNumber];
i = i + 1;
end
%for i=1:10
results_cell{i,1} = shuffledMatrix(trainRows ,:);
results_cell{i,2} = shuffledMatrix(testRows ,:);
end
end
data
5.1000 3.5000 1.4000 0.2000
4.9000 3.0000 1.4000 0.2000
4.7000 3.2000 1.3000 0.2000
4.6000 3.1000 1.5000 0.2000
5.0000 3.6000 1.4000 0.2000
5.4000 3.9000 1.7000 0.4000
4.6000 3.4000 1.4000 0.3000
5.0000 3.4000 1.5000 0.2000
4.4000 2.9000 1.4000 0.2000
4.9000 3.1000 1.5000 0.1000
5.4000 3.7000 1.5000 0.2000
4.8000 3.4000 1.6000 0.2000
4.8000 3.0000 1.4000 0.1000
4.3000 3.0000 1.1000 0.1000
5.8000 4.0000 1.2000 0.2000
5.7000 4.4000 1.5000 0.4000
5.4000 3.9000 1.3000 0.4000
5.1000 3.5000 1.4000 0.3000
5.7000 3.8000 1.7000 0.3000
5.1000 3.8000 1.5000 0.3000
5.4000 3.4000 1.7000 0.2000
5.1000 3.7000 1.5000 0.4000
4.6000 3.6000 1.0000 0.2000
5.1000 3.3000 1.7000 0.5000
4.8000 3.4000 1.9000 0.2000
5.0000 3.0000 1.6000 0.2000
5.0000 3.4000 1.6000 0.4000
5.2000 3.5000 1.5000 0.2000
5.2000 3.4000 1.4000 0.2000
4.7000 3.2000 1.6000 0.2000
4.8000 3.1000 1.6000 0.2000
5.4000 3.4000 1.5000 0.4000
5.2000 4.1000 1.5000 0.1000
5.5000 4.2000 1.4000 0.2000
4.9000 3.1000 1.5000 0.1000
5.0000 3.2000 1.2000 0.2000
5.5000 3.5000 1.3000 0.2000
4.9000 3.1000 1.5000 0.1000
4.4000 3.0000 1.3000 0.2000
5.1000 3.4000 1.5000 0.2000
5.0000 3.5000 1.3000 0.3000
4.5000 2.3000 1.3000 0.3000
4.4000 3.2000 1.3000 0.2000
5.0000 3.5000 1.6000 0.6000
5.1000 3.8000 1.9000 0.4000
4.8000 3.0000 1.4000 0.3000
5.1000 3.8000 1.6000 0.2000
4.6000 3.2000 1.4000 0.2000
5.3000 3.7000 1.5000 0.2000
5.0000 3.3000 1.4000 0.2000
7.0000 3.2000 4.7000 1.4000
6.4000 3.2000 4.5000 1.5000
6.9000 3.1000 4.9000 1.5000
5.5000 2.3000 4.0000 1.3000
6.5000 2.8000 4.6000 1.5000
5.7000 2.8000 4.5000 1.3000
6.3000 3.3000 4.7000 1.6000
4.9000 2.4000 3.3000 1.0000
6.6000 2.9000 4.6000 1.3000
5.2000 2.7000 3.9000 1.4000
5.0000 2.0000 3.5000 1.0000
5.9000 3.0000 4.2000 1.5000
6.0000 2.2000 4.0000 1.0000
6.1000 2.9000 4.7000 1.4000
5.6000 2.9000 3.6000 1.3000
6.7000 3.1000 4.4000 1.4000
5.6000 3.0000 4.5000 1.5000
5.8000 2.7000 4.1000 1.0000
6.2000 2.2000 4.5000 1.5000
5.6000 2.5000 3.9000 1.1000
5.9000 3.2000 4.8000 1.8000
6.1000 2.8000 4.0000 1.3000
6.3000 2.5000 4.9000 1.5000
6.1000 2.8000 4.7000 1.2000
6.4000 2.9000 4.3000 1.3000
6.6000 3.0000 4.4000 1.4000
6.8000 2.8000 4.8000 1.4000
6.7000 3.0000 5.0000 1.7000
6.0000 2.9000 4.5000 1.5000
5.7000 2.6000 3.5000 1.0000
5.5000 2.4000 3.8000 1.1000
5.5000 2.4000 3.7000 1.0000
5.8000 2.7000 3.9000 1.2000
6.0000 2.7000 5.1000 1.6000
5.4000 3.0000 4.5000 1.5000
6.0000 3.4000 4.5000 1.6000
6.7000 3.1000 4.7000 1.5000
6.3000 2.3000 4.4000 1.3000
5.6000 3.0000 4.1000 1.3000
5.5000 2.5000 4.0000 1.3000
5.5000 2.6000 4.4000 1.2000
6.1000 3.0000 4.6000 1.4000
5.8000 2.6000 4.0000 1.2000
5.0000 2.3000 3.3000 1.0000
5.6000 2.7000 4.2000 1.3000
5.7000 3.0000 4.2000 1.2000
5.7000 2.9000 4.2000 1.3000
6.2000 2.9000 4.3000 1.3000
5.1000 2.5000 3.0000 1.1000
5.7000 2.8000 4.1000 1.3000
6.3000 3.3000 6.0000 2.5000
5.8000 2.7000 5.1000 1.9000
7.1000 3.0000 5.9000 2.1000
6.3000 2.9000 5.6000 1.8000
6.5000 3.0000 5.8000 2.2000
7.6000 3.0000 6.6000 2.1000
4.9000 2.5000 4.5000 1.7000
7.3000 2.9000 6.3000 1.8000
6.7000 2.5000 5.8000 1.8000
7.2000 3.6000 6.1000 2.5000
6.5000 3.2000 5.1000 2.0000
6.4000 2.7000 5.3000 1.9000
6.8000 3.0000 5.5000 2.1000
5.7000 2.5000 5.0000 2.0000
5.8000 2.8000 5.1000 2.4000
6.4000 3.2000 5.3000 2.3000
6.5000 3.0000 5.5000 1.8000
7.7000 3.8000 6.7000 2.2000
7.7000 2.6000 6.9000 2.3000
6.0000 2.2000 5.0000 1.5000
6.9000 3.2000 5.7000 2.3000
5.6000 2.8000 4.9000 2.0000
7.7000 2.8000 6.7000 2.0000
6.3000 2.7000 4.9000 1.8000
6.7000 3.3000 5.7000 2.1000
7.2000 3.2000 6.0000 1.8000
6.2000 2.8000 4.8000 1.8000
6.1000 3.0000 4.9000 1.8000
6.4000 2.8000 5.6000 2.1000
7.2000 3.0000 5.8000 1.6000
7.4000 2.8000 6.1000 1.9000
7.9000 3.8000 6.4000 2.0000
6.4000 2.8000 5.6000 2.2000
6.3000 2.8000 5.1000 1.5000
6.1000 2.6000 5.6000 1.4000
7.7000 3.0000 6.1000 2.3000
6.3000 3.4000 5.6000 2.4000
6.4000 3.1000 5.5000 1.8000
6.0000 3.0000 4.8000 1.8000
6.9000 3.1000 5.4000 2.1000
6.7000 3.1000 5.6000 2.4000
6.9000 3.1000 5.1000 2.3000
5.8000 2.7000 5.1000 1.9000
6.8000 3.2000 5.9000 2.3000
6.7000 3.3000 5.7000 2.5000
6.7000 3.0000 5.2000 2.3000
6.3000 2.5000 5.0000 1.9000
6.5000 3.0000 5.2000 2.0000
6.2000 3.4000 5.4000 2.3000
5.9000 3.0000 5.1000 1.8000
labels =
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3