Split a vector into smaller vectors in matlab - matlab

Are there any efficient way to do the following?
I have this vector:
0.923
0.757
0.552
0.298
0.079
0.925
0.769
0.565
0.297
0.075
0.927
0.777
0.572
0.294
0.072
0.931
0.778
0.57
0.292
0.07
0.933
0.78
0.566
0.293
0.075
I want to split this vector to smaller vectors each one consist of 5 values, and adding 1 in the top and 0 at the end of each vector
like this:
1
0.923
0.757
0.552
0.298
0.079
0
1
0.925
0.769
0.565
0.297
0.075
0
1
0.927
0.777
0.572
0.294
0.072
0
1
0.931
0.778
0.57
0.292
0.07
0
1
0.933
0.78
0.566
0.293
0.075
0
can I use cumsum to find the difference between value 1 and 2 in the same vector ?
for example, the first vector
0.923 - 1 = 0.077
and form another vector with the answers ?

v =[0.923
0.757
0.552
0.298
0.079
0.925
0.769
0.565
0.297
0.075
0.927
0.777
0.572
0.294
0.072
0.931
0.778
0.57
0.292
0.07
0.933
0.78
0.566
0.293
0.075];
A = reshape(v,5,[]);
A = [ones(1,size(A,2)) ; A ; zeros(1,size(A,2))]
A =
1.00000 1.00000 1.00000 1.00000 1.00000
0.92300 0.92500 0.92700 0.93100 0.93300
0.75700 0.76900 0.77700 0.77800 0.78000
0.55200 0.56500 0.57200 0.57000 0.56600
0.29800 0.29700 0.29400 0.29200 0.29300
0.07900 0.07500 0.07200 0.07000 0.07500
0.00000 0.00000 0.00000 0.00000 0.00000
B = diff(A)
B =
-0.077000 -0.075000 -0.073000 -0.069000 -0.067000
-0.166000 -0.156000 -0.150000 -0.153000 -0.153000
-0.205000 -0.204000 -0.205000 -0.208000 -0.214000
-0.254000 -0.268000 -0.278000 -0.278000 -0.273000
-0.219000 -0.222000 -0.222000 -0.222000 -0.218000
-0.079000 -0.075000 -0.072000 -0.070000 -0.075000

Related

Remove zeros from a matrix

I have a matrix with large rows and columns as follows:
A = 0 0 0 0
0 0 0 0
0 0 0 0
2000 11 16 -0.74
0 0 0 0
0 0 0 0
2000 12 26 -0.84
0 0 0 0
0 0 0 0
I need to remove all the zeros from the matrix to get output like,
B = 2000 11 16 -0.74
2000 12 26 -0.84
I have tried an available solution over here like,
B = A(A~=0)
It removes zeros but gives output like,
2000
2000
11
12
-0.74
-0.84
How to get the desired output?
assuming A is a two dimensional matrix
A(any(A,2),:)
will do.
Example:
>> A=[rand(2,3); zeros(3); rand(1,3)]
A =
0.13878 0.44315 0.25832
0.01879 0.93844 0.57537
0.00000 0.00000 0.00000
0.00000 0.00000 0.00000
0.00000 0.00000 0.00000
0.50581 0.37870 0.56563
>> A(any(A,2),:)
ans =
0.138776 0.443152 0.258325
0.018794 0.938439 0.575371
0.505809 0.378696 0.565632

Why i didn't get the complete decision tree(I mean all attributes)?

I used decision tree classifier in MATLAB. I used 7 attributes.But when i draw the decision tree it doesn't includes all attributes,it only includes 2 or 1 attributes.What is the problem of my code?
vars = {'Asymmetry' 'Border irregularity' 'colors' 'contrast' 'Co-relation'
'Homogeneity' 'Energy'};
x = [0.148 0.298 3 0.027 0.959 0.992 0.692
0.248 0.462 3 0.015 0.997 0.996 0.837
0.683 0.827 3 0.030 0.974 0.989 0.634
0.170 0.509 3 0.065 0.964 0.977 0.399
0.663 0.764 3 0.061 0.945 0.983 0.645
0.641 0.671 3 0.050 0.953 0.987 0.703
0.653 0.796 2 0.062 0.961 0.981 0.528
0.458 0.704 2 0.019 0.934 0.993 0.852
0.555 0.729 2 0.087 0.976 0.980 0.380
0.454 0.657 2 0.059 0.953 0.982 0.467
0.379 0.497 2 0.058 0.976 0.979 0.445
0.443 0.486 2 0.034 0.896 0.998 0.810
0.194 0.342 2 0.012 0.956 0.997 0.895
0.248 0.462 3 0.015 0.977 0.996 0.837
0.155 0.340 2 0.010 0.930 0.966 0.911
0.458 0.704 2 0.019 0.934 0.993 0.852];
y = {'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'Cancer';'non-Cancer';'non-Cancer';'non-Cancer';'non-Cancer'};
t = fitctree(x,y,'PredictorNames',vars, ...
'CategoricalPredictors',{},'Prune','off');
view(t);
X1=[0.148 0.186 2 0.139 0.984 0.992 0.558]
label = predict(t,X1);
view(t,'mode','graph');
The output image of the code:
There's nothing wrong with your code - the decision tree is not designed to use all of your variables, it's just designed to use the variables that give the best fit based on the given decision criteria. Using all of the variables would result in overfitting, especially considering that some of your variables are correlated with each other.

Combining columns of two matrices of different dimension in Matlab?

I have a matrix in Matlab A of dimension nx3, e.g. n=8
A=[ 0.3 2 2;
0.3 7 7;
0.3 10 10;
0 15 15;
0.3 18 2;
0.3 23 7;
0 26 10;
0.3 31 15]
and a matrix B of dimension mx4, e.g. m=17
B=[1 1 0.05 0.05;
2 2 0.22 0.22;
3 3 0.19 0.05;
5 5 0.02 0.02;
6 6 0.19 0 ;
7 7 0.30 0.11;
10 10 0.27 0.08;
11 11 0.19 0 ;
12 12 0.05 0.05;
18 2 0.25 0.08;
19 3 0.25 0.08;
21 5 0.02 0.02;
22 6 0.22 0.08;
23 7 0.22 0.08;
30 14 0.19 0.08;
31 15 0.19 0.08;
32 16 0.05 0.05]
I want to create a matrix C following these steps WITHOUT USING LOOPS:
1) Generate C=[];
2) Consider B(i,1). If there exists A(j,2)=B(i,1) [it can happen only once] report C=[ C; B(i,1) B(i,2) B(i,3) B(i,4) A(i,1)]. Do this for i=1,...,m.
3) Consider B(h,1) such that there is no j with A(j,2)=B(h,1). Report C=[C; B(h,1) B(h,2) B(h,3) B(h,4) 0]. Do this for h=1,...,m.
4) Consider A(h,2) such that there is no j with B(j,1)=A(h,2). Report C=[C; A(h,2) A(h,3) 0 0 A(h,1)]. Do this for h=1,...,n.
In the example above I want to get
C=[2 2 0.22 0.22 0.3;
7 7 0.30 0.11 0.3;
10 10 0.27 0.08 0.3;
18 2 0.25 0.08 0.3;
23 7 0.22 0.08 0.3;
31 15 0.19 0.08 0.3; %end step 2)
---------------------
1 1 0.05 0.05 0 ;
3 3 0.19 0.05 0 ;
5 5 0.02 0.02 0 ;
6 6 0.19 0 0 ;
11 11 0.19 0 0 ;
12 12 0.05 0.05 0 ;
19 3 0.25 0.08 0 ;
21 5 0.02 0.02 0 ;
22 6 0.22 0.08 0 ;
30 14 0.19 0.08 0 ;
32 16 0.05 0.05 0 ;
----------------------- %end step 3)
15 15 0 0 0 ;
26 10 0 0 0 ] %end step 4)
These code does what I want but it is too slow with bigger matrices
C=[];
%Step 1)
for l=1:size(B,1)
for h=1:size(A,1)
if B(l,1)==A(h,2)
C=[C; B(l,:) A(h,1)];
end
end
end
% Steps 2) and 3)
C=[C; ...
[B(logical(1-ismember(B(:,1), A(:,2))),:) zeros(size(B(logical(1-ismember(B(:,1), A(:,2))),:),1),1)];...
[A(logical(1-ismember(A(:,2), B(:,1))),2:3) ...
zeros(size(A(logical(1-ismember(A(:,2), B(:,1)))),1),2) ...
A(logical(1-ismember(A(:,2), B(:,1))),1)]];
Although it smells strongly like homework, here is some code. See it as a tutorial on matrix operations (tested with Octave).
% Step 1
[~,j,k] = intersect(B(:,1),A(:,2));
C = [B(j,:) A(k,1)];
% Step 2
[~,k] = setdiff(B(:,1),A(:,2));
C = [C; B(k,:) zeros(size(k,1),1)]
% Step 3
[~,k] = setdiff(A(:,2),B(:,1));
C = [C; A(k,[2 3]) zeros(size(k,1),2) A(k,1)]
C =
2.00000 2.00000 0.22000 0.22000 0.30000
7.00000 7.00000 0.30000 0.11000 0.30000
10.00000 10.00000 0.27000 0.08000 0.30000
18.00000 2.00000 0.25000 0.08000 0.30000
23.00000 7.00000 0.22000 0.08000 0.30000
31.00000 15.00000 0.19000 0.08000 0.30000
1.00000 1.00000 0.05000 0.05000 0.00000
3.00000 3.00000 0.19000 0.05000 0.00000
5.00000 5.00000 0.02000 0.02000 0.00000
6.00000 6.00000 0.19000 0.00000 0.00000
11.00000 11.00000 0.19000 0.00000 0.00000
12.00000 12.00000 0.05000 0.05000 0.00000
19.00000 3.00000 0.25000 0.08000 0.00000
21.00000 5.00000 0.02000 0.02000 0.00000
22.00000 6.00000 0.22000 0.08000 0.00000
30.00000 14.00000 0.19000 0.08000 0.00000
32.00000 16.00000 0.05000 0.05000 0.00000
15.00000 15.00000 0.00000 0.00000 0.00000
26.00000 10.00000 0.00000 0.00000 0.00000

predict value of curve in matlab

Suppose I have the following vector of points:
X=[ 0.401 0.398 0.395 0.392 0.388 0.384 0.381 0.377 0.373 0.368 0.364 0.359 0.354 0.349 0.344 0.339 0.334 0.328 0.322 0.316 0.310 0.304 0.297 0.291 0.284 0.277 0.270 0.263 0.256 0.249 0.242 0.234 0.227 0.220 0.212 0.205 0.198 0.190 0.183 0.176 0.169 0.161 0.154 0.147 0.140 0.134 0.127 0.120 0.113 0.107 0.101 0.094 0.088 0.082 0.076 0.070 0.064 0.059 0.053 0.048 0.042 0.037 0.032 0.027 0.022 0.018 0.013 0.009 0.004 0.000 -0.004 -0.008 -0.012 -0.016 -0.019 -0.023 -0.026 -0.030 -0.033 -0.036 -0.039 -0.042 -0.045 -0.048 -0.050 -0.053 -0.055 -0.058 -0.060 -0.062 -0.064 -0.066 -0.068 -0.070 -0.072 -0.074 -0.076 -0.077 -0.079 -0.080];
Y=[0.347 0.362 0.377 0.393 0.409 0.426 0.442 0.459 0.477 0.494 0.512 0.530 0.548 0.567 0.585 0.604 0.622 0.641 0.659 0.678 0.696 0.715 0.733 0.750 0.768 0.785 0.801 0.817 0.833 0.848 0.863 0.876 0.890 0.902 0.914 0.925 0.935 0.945 0.953 0.961 0.969 0.975 0.981 0.986 0.990 0.993 0.996 0.998 0.999 1.000 1.000 0.999 0.998 0.996 0.994 0.991 0.988 0.984 0.979 0.974 0.969 0.963 0.957 0.951 0.944 0.937 0.930 0.922 0.914 0.906 0.898 0.889 0.881 0.872 0.863 0.855 0.846 0.837 0.827 0.818 0.809 0.800 0.791 0.782 0.773 0.764 0.755 0.747 0.738 0.729 0.721 0.712 0.704 0.696 0.688 0.680 0.672 0.664 0.656 0.649];
When I plot the points X and Y, this is what I get:
I want to calculate the value of 'Width' of the curve W. How can I do that?
It looks like the points are unordered, and so simply subtracting the last point by the first point won't work. What you can do is use max and min on the X array to determine the width:
Width = max(X) - min(X);
It's certainly as simple as that! FWIW, your title says one thing, but your question asks another. Suggest you either edit your question or title for clarity.

Matlab: Avoid exp(-3) conversion at text data input

I have a txt file with data, so I used the following function
M = dlmread('data.txt', '\t');
But My data is getting converted to exp(-3) how do I avoid that?
Here how the data look like:
M =
1.0e+03 *
0 0.0080 0.3500 0.1500 4.6990 0.0145 0.0740 0
0 0.0080 0.4000 0.1700 4.7460 0.0120 0.0710 0
0 0.0080 0.4000 0.1750 4.3850 0.0120 0.0720 0
0 0.0060 0.2500 0.0720 3.1580 0.0195 0.0750 0
0 0.0080 0.3040 0.1500 3.8920 0.0125 0.0720 0
0 0.0080 0.3500 0.1450 4.4400 0.0140 0.0750 0
0 0.0060 0.2500 0.1050 3.8970 0.0185 0.0750 0
0 0.0060 0.1630 0.1330 3.4100 0.0158 0.0780 0.0010
0 0.0080 0.2600 0.1100 4.0600 0.0190 0.0770 0
As H Muster says, this is a display issue.
A better way (in my opinion) of displaying your data is via num2str.
If you run
num2str(M)
you will see
ans =
0 0.008 0.35 0.15 4.699 0.0145 0.074 0
0 0.008 0.4 0.17 4.746 0.012 0.071 0
0 0.008 0.4 0.175 4.385 0.012 0.072 0
0 0.006 0.25 0.072 3.158 0.0195 0.075 0
0 0.008 0.304 0.15 3.892 0.0125 0.072 0
0 0.008 0.35 0.145 4.44 0.014 0.075 0
0 0.006 0.25 0.105 3.897 0.0185 0.075 0
0 0.006 0.163 0.133 3.41 0.0158 0.078 0.001
0 0.008 0.26 0.11 4.06 0.019 0.077 0
which is probably what you were expecting in the first place.
If you want more precision, pass a format string to num2str.
For example:
num2str(M,'%8g')