How to delete the repeated data of txt using Matlab? - matlab

I want to delete the repeat data in the flowing. Then ascending order according to the first column. Could you show the detail code to solve this problem? thank you very much.
15.0 0.8 1.6 -0.0 -0.3
16.0 2.4 -0.2 0.1 -0.1
17.0 2.6 -0.6 0.2 0.2
18.0 4.6 1.0 0.3 0.1
19.0 2.3 1.4 0.9 0.2
20.0 2.4 0.9 0.5 0.4
6.0 5.8 2.5 -1.8 -1.9
7.0 4.1 4.1 -0.8 1.6
8.0 2.8 2.3 2.2 -0.5
9.0 -1.5 -1.9 1.1 0.9
10.0 0.9 0.2 0.0 0.1
9.0 -1.5 -1.9 1.1 0.9
10.0 0.9 0.2 0.0 0.1
11.0 2.3 -3.6 0.7 -0.6
12.0 -1.2 -0.4 -0.2 -0.1
13.0 -4.4 0.3 -0.3 -0.2
14.0 -0.9 0.9 -0.4 -0.4
15.0 0.8 1.6 -0.0 -0.3

From the documentation of unique, we find that the unique(B, 'rows') command will
output unique rows only
sort the output
So:
B = [...
your data here
];
C = unique(B, 'rows');
will solve both your problems: the default for sort is to sort down the first column.

Related

find maximum of 3 other rows in same array using 'for' for each value

I have 3 results for the same problems in the following array:
(0.0 0.0 0.2
0.8 0.0 1.2
2.9 1.6 4.3
0.0 1.5 0.2
0.0 0.0 0.4
0.0 2.4 0.3
0.4 0.3 6.2
0.9 0.0 0.4
0.3 0.0 1.2)
For example, one problem has the results 0.0 0.0 0.4 in positions (1,1) (4,1) (7,1). I am trying to find the maximum result in order to have only a 3x3 array:
for m=1:3
for n=1:3
sy(m,n)=max(sy(m,n), sy(m+3,n), sy(m+3*2,n));
end
end
sy=imresize(sy, [3 3]);
And I get MAX with two matrices to compare and a working dimension is
not supported.
Assuming that your matrix is set up as follows:
sy =[0.0 0.0 0.2; 0.8 0.0 1.2; 2.9 1.6 4.3; 0.0 1.5 0.2 ;...
0.0 0.0 0.4; 0.0 2.4 0.3; 0.4 0.3 6.2; 0.9 0.0 0.4; 0.3 0.0 1.2];
Then I think:
sy = max(reshape(sy',3,3,3),[],3)';
Achieves your desired result.

How can I find the average of largest set of non-zero values in an array

How can I find the the starting point of A array and calculate average starting from starting points to 1 second
A=[0 0 0 0 0 -0.01 -0.2 0.3 0.4 0.5 0 0 0 0 0 0 0.01 0.02 0.03 0.04 0.1 0.2 0.3 0.4 0.7 0.8 1 1.2 1.3 1.4 1.5]
Time=[0 0.1 .2 .3 .4 .5 .6 .7 .8 .9 1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.8 3 3.1]
By removing the noise the starting point should be A(17) which is equal to 0.01
Then calculate average of A starting from starting point after 1 seconds
Code is self explanatory
A=[0 0 0 0 0 -0.01 -0.2 0.3 0.4 0.5 0 0 0 0 0 0 0.01 0.02 0.03 0.04 0.1 0.2 0.3 0.4 0.7 0.8 1 1.2 1.3 1.4 1.5] ;
Time=[0 0.1 .2 .3 .4 .5 .6 .7 .8 .9 1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.8 3 3.1];
%make negative values zero
A(A<0)=0;
%get non negative values position and add padding
mask=[0,A>0,0];
%get starting points
startingPoints =strfind(mask,[0 1]);
%get length of continuous values from starting points
temp =diff(find(~mask))-1;
length = temp(temp>0);
%get the index of largest length
[~,index]=max(length);
%get starting point
dataStartingIndex = startingPoints(index)
%starting point value
A(dataStartingIndex)
%get ending point after 1 seconds
dataEndingIndex=find((Time(dataStartingIndex)+1)==Time);
%find average
avg=mean(A(dataStartingIndex:dataEndingIndex))
This really depends on your data. It is a bit unclear but in your example it seems that noise can exceed your 'information value'. So you can't detect it just with a threshold.
Maybe get the position where A is always superior to something like 0.01 :
startpos= (A>0).argmax()
truedata=A[startpos:]
time=T[startpos:]
you can calculate average with the method .mean()

reshape matrix in matlab

I don't see the bug anymore...maybe (very probably :-) ) there's even a much more easier and faster way of doing it...
I summarized the important columns of my huge data frame in a little expData (see below).
The problem is actually quite easy, but I'm just blind for the easy idea of solving it..
My objective is to reshape columns b,c,d into one column that expData afterwards looks like expData2.
I would be really happy, if someone could help me out.
My code so far:
a = [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]';
b = [0.3 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.5 0.8 0.8 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]';
c = [0.4 0.4 0.4 0.4 0.4 0.6 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9 0.1 0.1 0.1 0.1 0.1]';
d = [0.5 0.5 0.5 0.5 0.5 0.1 0.1 0.1 0.1 0.1 0.7 0.7 0.7 0.7 0.7 0.2 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.3 0.3]';
e = rand(25,1);
f = rand(25,1);
a2 = [2 3 4 2 3 4 2 3 4 2 3 4 2 3 4]';
b2 = [0.3 0.4 0.5 0.4 0.6 0.1 0.5 0.8 0.7 0.8 0.9 0.2 0.9 0.1 0.3]';
c2 = rand(15,1);
d2 = rand(15,1);
expData = horzcat(a,b,c,d,e,f);
expData2 = horzcat(a2,b2,c2,d2); % for explanation of my objective
k = horzcat(expData(:,2),expData(:,3),expData(:,4))'; % How I wanted to do it
expData(:,2:4) = [];
k = reshape(k,[],1);
for index = 1:size(expData,1)
if expData(index,1) == 1
expData(index,:) = [];
end
if expData(index,1) == 5
expData(index,:) = [];
end
end
k = k(1:size(expData,1),:);
expData2 = [expData k];
Your current code throws an error, since the number of loop iterations gets determined at the beginning of the loop. As you are removing rows of expData, you run out of rows to index at some point.
The quick fix would be to start looping from the back, i.e. use for index = size(expData,1):-1:1. This way, you can safely remove rows without running into indexing problems.
The elegant fix is to use ismember to identify rows to remove:
rows2remove = ismember(expData(:,1),[1 5]);
expDate(rows2remove,:) = [];

Plot bar in matlab with log-scale x axis and same width

I want to plot a bar chart in Matlab with (1) log-scale for x-axis and (2)bars in same width. But with the code below, the width of the bars are different. Can any one help? Many thanks!
xdata = [0.01 0.018 0.032 0.056 0.1 0.18 0.32 0.56 1 1.8 3.2 5.6 10];
ydata = [1.3 1.6 1.5 1.2 1.0 3.5 0.6 3.1 1.6 1.9 1.7 0.3 0.4];
bar(xdata,ydata);
set(gca,'XScale','log');
Instead of plotting xdata on a log scale, plot the log of xdata on a linear scale. Then modify labels to reflect the linear value (not the used log value).
xdata = [0.01 0.018 0.032 0.056 0.1 0.18 0.32 0.56 1 1.8 3.2 5.6 10];
ydata = [1.3 1.6 1.5 1.2 1.0 3.5 0.6 3.1 1.6 1.9 1.7 0.3 0.4];
bar(log10(xdata),ydata);
set(gca,'Xtick',-3:1); %// adjust manually; values in log scale
set(gca,'Xticklabel',10.^get(gca,'Xtick')); %// use labels with linear values

I need to program in Matlab do loops

I have four parameters Q1,Q2,Q3,Q4 and I want to increase the parameters sequentiality 0.05
I mean I want to start the four parameters with the value 0.1, then increase Q1 by 0.05 and keep the others as it 0.1. then keep Q1=0.15, increase the second one Q2 0.05 a
From what I understood, you want to generate all possible tuples from the four parameters, where each can go from 0.1 to some higher value (I am using 0.2) in 0.05 increases. If this is the case, you can use the NDGRID function:
[q1,q2,q3,q4] = ndgrid(0.1:0.05:0.2);
params = [q1(:) q2(:) q3(:) q4(:)]
The result as rows:
params =
0.1 0.1 0.1 0.1
0.15 0.1 0.1 0.1
0.2 0.1 0.1 0.1
0.1 0.15 0.1 0.1
0.15 0.15 0.1 0.1
0.2 0.15 0.1 0.1
0.1 0.2 0.1 0.1
0.15 0.2 0.1 0.1
0.2 0.2 0.1 0.1
0.1 0.1 0.15 0.1
0.15 0.1 0.15 0.1
0.2 0.1 0.15 0.1
0.1 0.15 0.15 0.1
0.15 0.15 0.15 0.1
0.2 0.15 0.15 0.1
0.1 0.2 0.15 0.1
0.15 0.2 0.15 0.1
0.2 0.2 0.15 0.1
0.1 0.1 0.2 0.1
0.15 0.1 0.2 0.1
0.2 0.1 0.2 0.1
0.1 0.15 0.2 0.1
0.15 0.15 0.2 0.1
0.2 0.15 0.2 0.1
0.1 0.2 0.2 0.1
0.15 0.2 0.2 0.1
0.2 0.2 0.2 0.1
0.1 0.1 0.1 0.15
0.15 0.1 0.1 0.15
0.2 0.1 0.1 0.15
0.1 0.15 0.1 0.15
0.15 0.15 0.1 0.15
0.2 0.15 0.1 0.15
0.1 0.2 0.1 0.15
0.15 0.2 0.1 0.15
0.2 0.2 0.1 0.15
0.1 0.1 0.15 0.15
0.15 0.1 0.15 0.15
0.2 0.1 0.15 0.15
0.1 0.15 0.15 0.15
0.15 0.15 0.15 0.15
0.2 0.15 0.15 0.15
0.1 0.2 0.15 0.15
0.15 0.2 0.15 0.15
0.2 0.2 0.15 0.15
0.1 0.1 0.2 0.15
0.15 0.1 0.2 0.15
0.2 0.1 0.2 0.15
0.1 0.15 0.2 0.15
0.15 0.15 0.2 0.15
0.2 0.15 0.2 0.15
0.1 0.2 0.2 0.15
0.15 0.2 0.2 0.15
0.2 0.2 0.2 0.15
0.1 0.1 0.1 0.2
0.15 0.1 0.1 0.2
0.2 0.1 0.1 0.2
0.1 0.15 0.1 0.2
0.15 0.15 0.1 0.2
0.2 0.15 0.1 0.2
0.1 0.2 0.1 0.2
0.15 0.2 0.1 0.2
0.2 0.2 0.1 0.2
0.1 0.1 0.15 0.2
0.15 0.1 0.15 0.2
0.2 0.1 0.15 0.2
0.1 0.15 0.15 0.2
0.15 0.15 0.15 0.2
0.2 0.15 0.15 0.2
0.1 0.2 0.15 0.2
0.15 0.2 0.15 0.2
0.2 0.2 0.15 0.2
0.1 0.1 0.2 0.2
0.15 0.1 0.2 0.2
0.2 0.1 0.2 0.2
0.1 0.15 0.2 0.2
0.15 0.15 0.2 0.2
0.2 0.15 0.2 0.2
0.1 0.2 0.2 0.2
0.15 0.2 0.2 0.2
0.2 0.2 0.2 0.2
If I understand this correctly, you want to increase your four parameters one at a time? How about you use a simple modulus statement, like this:
for (int i = 0; i < 100; i++) {
if (i % 4 == 0) {
Q1 += 0.05;
} else if (i % 4 == 1) {
Q2 += 0.05;
} else if (i % 4 == 2) {
Q3 += 0.05;
} else {
Q4 += 0.05;
}
}
That way, after one loop you will have:
Q1 = 0.15
Q2 = 0.10
Q3 = 0.10
Q4 = 0.10
But after two, you have:
Q1 = 0.15
Q2 = 0.15
Q3 = 0.10
Q4 = 0.10
And so on...
Also, with Matlab without for loop and if statements:
n=4;
step=0.05;
base=floor((2:1/4:n));
q=base*step;
q1=q(4:end);
q2=q(3:end-1);
q3=q(2:end-2);
q4=q(1:end-3);
Which produces:
0.10 0.15 0.15 0.15 0.15 0.20
0.10 0.10 0.15 0.15 0.15 0.15
0.10 0.10 0.10 0.15 0.15 0.15
0.10 0.10 0.10 0.10 0.15 0.15