I am trying to plot data from an Excel file. I am reading from the second sheet, which has names of machines (machine numbers, but it needs to be flexible), on the top and then numbers underneath.
Machine1
1
2
3
4
5
6
etc.
The first row, which has the names, needs to be the name of each graph.
Here is what I have so far. Also, please no advanced functions.
clear variables
clc
close all
i = 0;
n = 1;
% All the code to do the first part of the graphing
inputFile = xlsread('machineOutput.xlsx', 'Sheet2');
[rows, cols] = size(inputFile);
for c = 1: cols
clear tempVar;
clear tempName;
count = 0;
i = 1;
for r = 1: rows
if(isnan(inputFile(r, c)) == 1)
inputFile(r, c) = 0;
end
tempVar{n} = inputFile(r, c);
tempName{i} = inputFile(r, c);
end
fplot(tempVar, [-2 2])
title(tempName)
grid on
xlabel('Number of Days')
ylabel('All machines')
figure
xlim tight
n = n + 1;
end
Related
I'm trying to create a band matriz in Matlab, which should looks like this for matrix_size = 6 and band_width = 1:
[1,1,0,0,0,0]
[1,2,2,0,0,0]
[0,2,3,3,0,0]
[0,0,3,4,4,0]
[0,0,0,4,5,5]
[0,0,0,0,5,6]
The values should be like this.
I did function, which gives me result:
[1,1,0,0,0,0]
[1,1,1,0,0,0]
[0,1,1,1,0,0]
[0,0,1,1,1,0]
[0,0,0,1,1,1]
[0,0,0,0,1,1]
Code of my function:
function M=bandmatrix(n,r)
% n -- matriz size
% r -- band width
% n >= r + 2
M = sign(conv2(eye(n),ones(r+1),'same'));
end
How I can do this function? I also would be grateful for the function, in which walues are the same as I want, but function doesn't depends on band width. Thank you!
you can use diag as follows:
diag(1:6)+diag(1:5,1)+diag(1:5,-1)
Generally, for any order n:
diag(1:n)+diag(1:n-1,1)+diag(1:n-1,-1)
You could also do something like:
clear;
m = 6;
n = 6;
diags = repmat((1:6)', 1, 3);
diagidx = [-1:1];
for k = 1:length(diagidx)
if(diagidx(k) > 0)
diags(:, k) = circshift(diags(:, k), diagidx(k), 1);
end
end
sparseMat = spdiags(diags, [-1:1], m, n);
myMat = full(sparseMat)
Using sparse matrix allows us to specify the data and allocate only once.
But it also means the values used in the diagonal are needed to be shifted according to the matrix being fat or skinny.
Consider a Matlab matrix B which lists all possible unordered pairs (without repetitions) from [1 2 ... n]. For example, if n=4,
B=[1 2;
1 3;
1 4;
2 3;
2 4;
3 4]
Note that B has size n(n-1)/2 x 2
I want to take a random draw of m rows from B and store them in a matrix C. Continuing the example above, I could do that as
m=2;
C=B(randi([1 size(B,1)],m,1),:);
However, in my actual case, n=371293. Hence, I cannot create B and, then, run the code above to obtain C. This is because storing B would require a huge amount of memory.
Could you advise on how I could proceed to create C, without having to first store B? Comments on a different question suggest to
Draw at random m integers between 1 and n(n-1)/2.
I=randi([1 n*(n-1)/2],m,1);
Use ind2sub to obtain C.
Here, I'm struggling to implement the second step.
Thanks to the comments below, I wrote this
n=4;
m=10;
coord=NaN(m,2);
R= randi([1 n^2],m,1);
for i=1:m
[cr, cc]=ind2sub([n,n],R(i));
if cr>cc
coord(i,1)=cc;
coord(i,2)=cr;
elseif cr<cc
coord(i,1)=cr;
coord(i,2)=cc;
end
end
coord(any(isnan(coord),2),:) = []; %delete NaN rows from coord
I guess there are more efficient ways to implement the same thing.
You can use the function named myind2ind in this post to take random rows of all possible unordered pairs without generating all of them.
function [R , C] = myind2ind(ii, N)
jj = N * (N - 1) / 2 + 1 - ii;
r = (1 + sqrt(8 * jj)) / 2;
R = N -floor(r);
idx_first = (floor(r + 1) .* floor(r)) / 2;
C = idx_first-jj + R + 1;
end
I=randi([1 n*(n-1)/2],m,1);
[C1 C2] = myind2ind (I, n);
If you look at the odds, for i=1:n-1, the number of combinations where the first value is equal to i is (n-i) and the total number of cominations is n*(n-1)/2. You can use this law to generate the first column of C. The values of the second column of C can then be generated randomly as integers uniformly distributed in the range [i+1, n]. Here is a code that performs the desired tasks:
clc; clear all; close all;
% Parameters
n = 371293; m = 10;
% Generation of C
R = rand(m,1);
C = zeros(m,2);
s = 0;
t = n*(n-1)/2;
for i=1:n-1
if (i<n-1)
ind_i = R>=s/t & R<(s+n-i)/t;
else % To avoid rounding errors for n>>1, we impose (s+n-i)=t at the last iteration (R<(s+n-i)/t=1 always true)
ind_i = R>=s/t;
end
C(ind_i,1) = i;
C(ind_i,2) = randi([i+1,n],sum(ind_i),1);
s = s+n-i;
end
% Display
C
Output:
C =
84333 266452
46609 223000
176395 328914
84865 94391
104444 227034
221905 302546
227497 335959
188486 344305
164789 266497
153603 354932
Good luck!
[MATLAB]
I have a text file that is a list of numbers. Below is a sample but my actual file is a list of thousands of values, each on a new line.
0.01080000000
0.00720000000
0.05760000000
0.00360000000
How do I loop through this text file and input the data into a matrix of the size x = 431 and y = 415? Again, text file just has a list so every 431st number I need to go to a new row in my matrix.
clear;
%Load in text file
filename = 'Water_1973_points.txt';
T = fopen(filename);
%Count number of points in x,y (x = 431) (y = 415)
xsize = 431;
ysize = 415;
m=zeros(xsize, ysize);
tline = fgetl(T);
for k = 1:length(T)
for h = 1:xsize
for j = 1:ysize
m(h,j) = k*255;
end
end
end
If your file is that simple it's easy enough to just use importdata and reshape.
For example, using the following Water_1973_points.txt:
1
2
...
10
And
m = importdata('Water_1973_points.txt');
m = reshape(m, 5, 2).'; % Transpose because MATLAB is column-major
Returns:
m =
1 2 3 4 5
6 7 8 9 10
I want to read multiple text files. Each text file has two columns. All the two columns of all text files have same rows. I want to know, in MATLAB, how to read each text file then read each column one by one, subtract one column data from the other column and then read the next file and so on. I have written the following code but I am missing some step in the code. I appreciate your support. Thank you all.
for k = 1:9
filename = sprintf('Data_F_Ind000%d.txt',k);
a(:,k) = load(filename);
x = a(:,1)};
y = a(:,2);
z = x - y;
end
data = cell(9,1) ;
diff_data = cell(9,1) ;
for k = 1:9
filename = sprintf('Data_F_Ind000%d.txt',k);
a = load(filename);
data{i} = a ;
x = a(:,1)};
y = a(:,2);
diff_data{i} = x - y;
end
You can do this multiple ways. I imagine that you want to do something with z instead of just throwing it away every time. I would do this by taking advantage of an access pattern.
numFiles = 9;
numRows = ....; % not required but used to preallocate the a matrix
pattern = 1:2:numFiles * 2; % create a vector of 1 3 5 ...
a = zeros(numRows, numFiles * 2);
z = zeros(numRows, numFiles);
for k = 1:numFiles
fileName = sprintf('Data_F_Ind000%d.txt, 'k');
a(:,pattern(k):pattern(k) + 1) = load(fileName);
z(:,k) = a(:,pattern(k)) - a(:,pattern(k) + 1);
end
This is untested and is clearly missing some data but the intent should be clear. You don't need to preallocate variables but it helps speed calculations so I try to do it whenever possible.
In matlab usually we add a header using fprintf command.
This is a problem when the size of the table depends on the input and when it exceeds a certain range (more than total number of column able to be presented in the command window).
When this occurs the header which we specified previously using the fprintf command will not be compatible with the current output data.
I would like to know is there a way like adding a character string into the 1st row of the matrix during some kind of iteration process. I had tried hardly but still can't find a proper way to solve this issue.
Or it is actually cannot be done in matlab for this purpose.
Eg
clear;clc
A = [2 8 3 1;0 2 -1 4;7 -2 1 2;-1 0 5 2]
B = [-2;4;3;5]
Es = 1e-5
n = length(B);
x = zeros(n,1);
Ea = ones(n,1);
iter = 0;
while max(Ea) >= Es
if iter <= 30
iter = iter + 1;
x_old = x;
for i = 1:n
j = 1:n;
j(i) = [];
x_cal = x;
x_cal(i) = [];
x(i) = (B(i) - sum(A(i,j) * x_cal)) / A(i,i);
end
else
break
end
x_ans(:,iter) = x;
Ea(:,iter) =abs(( x - x_old) ./ x);
end
result = [1:iter; x_ans; Ea]'
for the coding above..how could I add the heading like iteration for 1st column, x1...x2...x3..xn for nth column and error x1..error x2..error xn for another n column. I would like to make this heading can be automated generated based on the input matrix
If the size of the table depends on the input, use a cell array, using c = cell(...).
In each iteration simply call c{i,j} instead of c[i,j].