I'm completely new to Matlab, so I've been tying out the various IO functions to see how they operate. Here I try to construct a matrix from a tab-delimited spreadsheet:
%Matrix test
M = dlmread('MyFile.txt', '\t', 1);
disp(M);
The output:
>> MatrixTest
Warning: Obsolete syntax. C must be specified with R.
> In dlmread (line 91)
In MatrixTest (line 2)
0.3800 0.2900 0.0400 0.2900 -22.2000
0.4600 0.4500 0.0200 0.0700 -22.2500
0.4900 0.1500 0.0200 0.3400 -66.7700
0.1000 0.8100 0.0200 0.0700 -81.7500
0.1200 0.5700 0.0500 0.2600 -49.5000
0.3000 0.6000 0.0300 0.0700 -57.4700
0.4200 0.0900 0.0100 0.4800 -56.3500
0.2600 0.2800 0.2900 0.1700 -79.7900
0.4800 0.0300 0.4000 0.0900 -76.8500
0.2600 0.0100 0.1400 0.5900 -28.6600
0.2900 0.5000 0.1900 0.0200 -21.5700
0.1400 0.8300 0.0200 0.0100 -31.2700
0.0200 0.4700 0.1300 0.3800 -93.1500
0.0400 0.5000 0.3500 0.1100 -16.9500
0.1100 0.0100 0.1300 0.7500 -11.0500
0.2000 0.6700 0.1200 0.0100 -44.7900
0.3900 0.3600 0.1700 0.0800 -18.7300
0.1500 0.3300 0.0600 0.4600 -48.4500
0.3900 0.0800 0.2300 0.3000 -60.6100
0.3400 0.3600 0.2200 0.0800 -33.0800
0.1400 0.7800 0.0200 0.0600 -60.5000
0.3600 0.6300 0.0100 0 -46.6700
0.1600 0.4800 0.0700 0.2900 -38.2800
The code works as expected, but apparently I've used obsolete syntax (or maybe the issue is with the dlmread function?). Anyway, what is the more appropriate way to perform this same operation?
Thanks in advance.
Try using this
%Matrix test
M = dlmread('MyFile.txt', '\t', 1, 0);
disp(M);
Here, for ignoring first column - use:
%Matrix test
M = dlmread('MyFile.txt', '\t', 1, 1);
disp(M);
Thanks
Related
Just working on multiply 2 rows of a matrix and making the answer another row within the same matrix. However when putting the new row in the matrix all of the values are for some reason divided by 1.0e+03.
X = (M(1,1:5) .* M(2,1:5));
M(3,1:5) = X(1,1:5)
disp(X)
Actual Result
X =
1.0e+03 *
0.4000 0.5500 0.7000 0.5000 0.6000
0.0030 0.0005 0.0008 0.0015 0.0050
1.2000 0.2750 0.5250 0.7500 3.0000
Expected Result
400.0000 550.0000 700.0000 500.0000 600.0000
3.0000 0.5000 0.7500 1.5000 5.0000
1200 275 525 750 3000
I would like to create a matrix to hold the result of functions in a nested loop as follow:
list = [0.01; 0.03; 0.1; 0.3; 1; 3; 10; 30];
res = zeros((size(list,1)),(size(list,1)));
for i = list
for j = list
res(i,j)=function(depending on i and j values from the list) goes
here); % This is the part where I need help
end
end
Because list contains real numbers the indexing res(i,j) doesn't work. Cn anyone give me an idea on how to proceed?
Thanks in advance.
All the suggested comments (working with indexes in a nested for) are valid for this answer, I will also recommend use something like this, the operation that you need is something simimlar to apply to a function to de cartesian product of the list so you can work as follow:
>> [X,Y] = meshgrid(list,list);
>> abs(X - Y)
ans =
0 0.0200 0.0900 0.2900 0.9900 2.9900 9.9900 29.9900
0.0200 0 0.0700 0.2700 0.9700 2.9700 9.9700 29.9700
0.0900 0.0700 0 0.2000 0.9000 2.9000 9.9000 29.9000
0.2900 0.2700 0.2000 0 0.7000 2.7000 9.7000 29.7000
0.9900 0.9700 0.9000 0.7000 0 2.0000 9.0000 29.0000
2.9900 2.9700 2.9000 2.7000 2.0000 0 7.0000 27.0000
9.9900 9.9700 9.9000 9.7000 9.0000 7.0000 0 20.0000
29.9900 29.9700 29.9000 29.7000 29.0000 27.0000 20.0000 0
This is the data, and a badly fitting exp2 fit:
http://oi64.tinypic.com/10e0di0.jpg
It seems easy to fit at first glance, but I have found it difficult to find a custom equation that fits this data. Exp2 and gauss2 are the usual candidates but I couldn't find a combination that work. It is important for me to preserve the monotony of the data, so polynomials make a bad fit, it is also important not to significantly overshoot or undershoot the data at the edges (like the shown exp2 fit). Preserving the sharply increasing middle part is less important.
Any help would be greatly appreciated.
Example data set:
x = 0 0.0200 0.0400 0.0600 0.0800 0.1000 0.1200 0.1400 0.1600 0.1800 0.2000 0.2200 0.2400 0.2600 0.2800 0.3000 0.3200 0.3400 0.3600 0.3800 0.4000 0.4200 0.4400 0.4600 0.4800 0.5000 0.5200 0.5400 0.5600 0.5800 0.6000 0.6200 0.6400 0.6600 0.6800 0.7000 0.7200 0.7400 0.7600 0.7800 0.8000 0.8200 0.8400 0.8600 0.8800 0.9000 0.9200 0.9400 0.9600 0.9800 1.0000
y = 0.0096 0.0093 0.0092 0.0092 0.0094 0.0097 0.0102 0.0107 0.0114 0.0123 0.0132 0.0144 0.0157 0.0171 0.0189 0.0209 0.0232 0.0259 0.0291 0.0328 0.0373 0.0426 0.0491 0.0570 0.0667 0.0789 0.0941 0.1135 0.1383 0.1699 0.2101 0.2605 0.3214 0.3918 0.4682 0.5455 0.6184 0.6834 0.7389 0.7848 0.8221 0.8521 0.8761 0.8954 0.9108 0.9232 0.9332 0.9413 0.9479 0.9533 0.9576
I calculated the function of interest $f(x,y)$ numerically in Mathematica. For consistency, I want to use Matlab for plotting.
I exported my function in the matrix form:
test =
-1.0000 -1.0000 -0.4864
-1.0000 -0.6000 -0.2804
-1.0000 -0.2000 -0.0462
-1.0000 0.2000 -0.0462
-1.0000 0.6000 -0.2804
-1.0000 1.0000 -0.4864
-0.6000 -1.0000 -0.2997
-0.6000 -0.6000 -0.1526
-0.6000 -0.2000 0.1118
-0.6000 0.2000 0.1118
-0.6000 0.6000 -0.1526
-0.6000 1.0000 -0.2997
-0.2000 -1.0000 -0.1809
-0.2000 -0.6000 -0.0939
-0.2000 -0.2000 -0.0046
-0.2000 0.2000 -0.0046
-0.2000 0.6000 -0.0939
-0.2000 1.0000 -0.1809
0.2000 -1.0000 -0.1809
0.2000 -0.6000 -0.0939
0.2000 -0.2000 -0.0046
0.2000 0.2000 -0.0046
0.2000 0.6000 -0.0939
0.2000 1.0000 -0.1809
0.6000 -1.0000 -0.2997
0.6000 -0.6000 -0.1526
0.6000 -0.2000 0.1118
0.6000 0.2000 0.1118
0.6000 0.6000 -0.1526
0.6000 1.0000 -0.2997
1.0000 -1.0000 -0.4864
1.0000 -0.6000 -0.2804
1.0000 -0.2000 -0.0462
1.0000 0.2000 -0.0462
1.0000 0.6000 -0.2804
1.0000 1.0000 -0.4864
So that test is 36x3 matrix, where the columns are x, y and f(x,y). Now I need to contour plot it.
However, it is not in the form of meshgrid. Any thoughts, how to quickly convert it in the form that can be plotted by contour function?
If your data is regular (data set coincide with meshgrid but in vector form), then you can use reshape.
xgrid=6;
ygrid=6;
contour(reshape(test(:,1),xgrid,ygrid),reshape(test(:,2),xgrid,ygrid),reshape(test(:,3),xgrid,ygrid))
Another option is tricontour which will also work for irregular dataset
tri = delaunay(test(:,1),test(:,2));
figure;
tricontour(tri,test(:,1),test(:,2),test(:,3));
You can find tricontour at https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data/content/tricontour.m
If your surface is not convex,
then you need to remove some of triangulation from tri because of the nature of delaunay
This question already has an answer here:
how to solve scatterplot error in matlab
(1 answer)
Closed 8 years ago.
May I know what is error shown below?
Error using scatter3 (line 77)
C must be a single color, a vector the same length as X, or an M-by-3 matrix.
It appear when I write a code:
scatter3(points(:,1),points(:,2),points(:,3),50,color,'fill');
The color value mentioned is display as below:
color =
0.6118 0.6118 0.6118
0.9216 0.7490 0.5216
1.0000 1.0000 1.0000
0.5922 0.5216 0.4118
0.9176 0.7020 0.4392
0.1412 0.1882 0.1569
0.7137 0.5176 0.3451
0.5765 0.3294 0.1176
0.6902 0.4471 0.2275
0.6039 0.5490 0.4078
1.0000 1.0000 1.0000
0.1529 0.2471 0.2314
0 0 0
0.4784 0.4902 0.3961
0 0 0
0.2039 0.1765 0.0745
1.0000 1.0000 1.0000
1.0000 1.0000 1.0000
0.1725 0.2314 0.2000
0.8118 0.5569 0.2980
0 0 0
0.9098 0.6902 0.4745
0.2588 0.2314 0.1176
0.0902 0.1490 0.1137
0.1725 0.1333 0.0784
0.5725 0.5843 0.5020
0 0 0
0.8510 0.6196 0.3843
0.3961 0.3608 0.2314
1.0000 1.0000 1.0000
0.4235 0.3843 0.2471
0.2824 0.2824 0.1294
0.6667 0.6745 0.6157
0.3020 0.2588 0.1451
0.0863 0.0627 0.0314
0.6039 0.5216 0.3843
0.7686 0.5804 0.3569
0.7608 0.5804 0.3569
0.2824 0.2000 0.0784
0.5255 0.4392 0.3490
0 0 0
0 0 0
0.2353 0.2275 0.1333
0 0 0
0.6118 0.5255 0.4275
0 0 0
0.6118 0.6118 0.6118
0.9216 0.7490 0.5216
1.0000 1.0000 1.0000
0.5922 0.5216 0.4118
0.9176 0.7020 0.4392
0.1412 0.1882 0.1569
0.7137 0.5176 0.3451
0.5765 0.3294 0.1176
0.6902 0.4471 0.2275
0.6039 0.5490 0.4078
1.0000 1.0000 1.0000
0.1529 0.2471 0.2314
0 0 0
0.4784 0.4902 0.3961
0 0 0
0.2039 0.1765 0.0745
1.0000 1.0000 1.0000
1.0000 1.0000 1.0000
0.1725 0.2314 0.2000
0.8118 0.5569 0.2980
0 0 0
0.9098 0.6902 0.4745
0.2588 0.2314 0.1176
0.0902 0.1490 0.1137
0.1725 0.1333 0.0784
0.5725 0.5843 0.5020
0 0 0
0.8510 0.6196 0.3843
0.3961 0.3608 0.2314
1.0000 1.0000 1.0000
0.4235 0.3843 0.2471
0.2824 0.2824 0.1294
0.6667 0.6745 0.6157
0.3020 0.2588 0.1451
0.0863 0.0627 0.0314
0.6039 0.5216 0.3843
0.7686 0.5804 0.3569
0.7608 0.5804 0.3569
0.2824 0.2000 0.0784
0.5255 0.4392 0.3490
0 0 0
0 0 0
0.2353 0.2275 0.1333
0 0 0
0.6118 0.5255 0.4275
0 0 0
0.6314 0.6431 0.6392
0.9059 0.7098 0.4941
0.7804 0.7804 0.7804
0.9333 0.7137 0.4784
0.7765 0.5294 0.2784
1.0000 1.0000 1.0000
0.6078 0.5176 0.3922
0.7373 0.5608 0.3333
0.7216 0.5176 0.3020
0.4353 0.4000 0.3608
1.0000 1.0000 1.0000
0.0627 0.1882 0.1412
0.2157 0.2118 0.2392
0.6392 0.4000 0.1922
0.1098 0.1647 0.1569
0.5765 0.5843 0.5373
0.6706 0.4235 0.2510
0.1412 0.1176 0.0471
0.5529 0.5569 0.5216
0 0 0
1.0000 1.0000 1.0000
0.8667 0.6588 0.4392
0.7804 0.5137 0.2941
1.0000 1.0000 1.0000
0.1686 0.1333 0.0588
0.8196 0.6000 0.3725
0.1961 0.1922 0.0902
0.5765 0.4980 0.3804
0.2039 0.2902 0.2667
0.4627 0.4039 0.2588
0.2706 0.2431 0.1216
1.0000 1.0000 1.0000
0.7686 0.5647 0.3451
0 0 0
0.5843 0.5137 0.4118
0.1569 0.1686 0.0941
0.2627 0.1451 0.0784
0.6627 0.4510 0.2706
0.2588 0.1961 0.1216
0.1686 0.1176 0.0510
0.5647 0.5686 0.5451
0.5608 0.5686 0.5255
0.5176 0.5373 0.4784
0.6549 0.4784 0.2784
0.5843 0.5922 0.5686
0.7490 0.7647 0.7333
0.1569 0.1255 0.0431
0.2471 0.2039 0.1451
I found the reason already. This is just because the number of color recoreded is not same with number of coordinates of color.