I previously asked for help in reading data from a text file generated by a C program (an Exe).
Using #second's solution, I solved the problem but yesterday I discovered that the output file is more complex than I had expected.
The file output is:
V|0|0|0|t|0|1|1|4|11|T4|H13||||||||||||
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0
*|A1|A1|A7|A16|F|F|F|F|F|F|||||||||||||
*|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc|||||||
*|||||kV|kV|kV|MW|MVAR|S||||||||||||
N|I|1|N01|N01|132|125.4|138.6|0|0||||||||
N|I|1|N02|N02|20|19|21|0|0|||||||||||||
N|I|1|N03|N03|20|19|21|1.013532234|0.49087611||||||||
N|I|1|N04|N04|20|19|21|0.390791617|0.189269056||||||||
N|I|1|N05|N05|20|19|21|0.180634542|0.121387171||||||||
N|I|1|N06|N06|20|19|21|0.709472564|0.343613323||||||||
N|I|1|N07|N07|20|19|21|0.103495727|0.069549543||||||||
N|I|1|N08|N08|20|19|21|0.351712456|0.170342158||||||||
N|I|1|N09|N09|20|19|21|0.097697904|0.06565339||||||||
N|I|1|N10|N10|20|19|21|0.162165157|0.078540184||||||||
N|I|1|N11|N11|20|19|21|0|0||||||||
*|A1|A8|A7|A7|F|F|F|F||F||F
*|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2|||
*|||||km|A|Ohm|Ohm||S||S
L|I|D10203|N02|N03|1.884|360|0.41071|0.207886957|3.19E-08|3.19E-08|||||||||||||
L|I|D10304|N03|N04|1.62|360|0.35316|0.1787563|3.19E-08|3.19E-08|||||||||||||
L|I|D10405|N04|N05|0.532|360|0.11598|0.058702686|3.19E-08|3.19E-08|||||||||||||
L|I|D10506|N05|N06|1.284|360|0.27991|0.14168092|3.19E-08|3.19E-08|||||||||||||
L|I|D10607|N06|N07|1.618|280|0.53879|0.194766124|3.00E-08|3.00E-08|||||||||||||
L|I|D10708|N07|N08|0.532|280|0.17716|0.064039294|3.00E-08|3.00E-08|||||||||||||
L|I|D10809|N08|N09|2|360|0.436|0.220686791|3.19E-08|3.19E-08|||||||||||||
L|I|D10910|N09|N10|2.4|360|0.5232|0.264824149|3.19E-08|3.19E-08||||||||||||
*|A1|A8|A7|A7|F|F|A1|F|F|F|F|F|F||F||F|||||||||||||||||||||||||
*|codserv|codice|estr1|estr2|vn1|vn2|nod1|varp|varm|np|Pb|rsd|xsd||bsd1||bsd2||||||||||||
*|||||kV|kV||%|%||MVA|%|%||%||%|||||
%%%%%------%%%%%------%%%% **(read up to here)**
other unnecessary data
The algorithm should:
skip the first 3 rows
skip fifth row
For the fourth row *|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc|||||||, save each string in a vector empty codeserv=[] area=[] codice=[] nome=[] tnom=[] tmin=[] tmax=[] pc=[] qc=[] susc=[]
Fill vectors with data and strings in the rows following the fourth
codeserv=[N N N N N N N N N N ....]
area=[I I I I I I I ....]
codice=[1 1 1 1 1 1 ...]
nome=[N01 N02 N03 N04 N05 ]
tnom=[N01 N02 N03 N04 N05]
tmin=[132 20 20.....]
tmax=[125.4 19 19 19 ....]
pc=[138.6 21 21 21....]
qc=[0 0 1.013532234 ....]
susc=[0 0 0.49087611]
Do the same with the data starting with the letter L. Read this line codice|estr1|estr2|lung|imax|rsd|xsd||bsd1||bsd2 and fill the vectors with values in the lines beginning with L
plev=[L L L L L L L ....]
area=[I I I I I I I ....]
codice=[D10203 D10304 ...]
estr1=[N02 N03 N04 N05 ...]
estr2=[N03 N04 N05...]
lung=[1.884 1,662 ....]
imax=[360 360 .....]
rsd=[number....]
xsd=[number....]
bsd1=[number ....]
bsd2=[number....]
I tried to adapt the code from the previous question, but given that the lines that start with N and L do not know how many I need to know how to read the first string and count the number N and L are.
read
[vp***NNNNNNNNNNNNNNNNNNNNNNNNLLLLLLLLLLLLLLLLLLLLL***]
length N
length L
skip 1 2 3 line
read 4 line, create vector
codeserv=[N N N N N N N N N N ....]
area=[I I I I I I I ....]
codice=[1 1 1 1 1 1 ...]
nome=[N01 N02 N03 N04 N05 ]
tnom=[N01 N02 N03 N04 N05]
tmin=[132 20 20.....]
tmax=[125.4 19 19 19 ....]
pc=[138.6 21 21 21....]
qc=[0 0 1.013532234 ....]
susc=[0 0 0.49087611]
skip length N +1 line
read *|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2|||
skip length N +3 line
create
plev=[L L L L L L L ....]
area=[I I I I I I I ....]
codice=[D10203 D10304 ...]
estr1=[N02 N03 N04 N05 ...]
estr2=[N03 N04 N05...]
lung=[1.884 1,662 ....]
imax=[360 360 .....]
rsd=[number....]
xsd=[number....]
bsd1=[number ....]
bsd2=[number....]
close the cycle
I hope it is understandable. My biggest problem is counting the N and L in the text.
function readtest2()
fid = fopen('test2.txt');
skipLines(3)
names1 = getNames;
skipLines(1);
nEntries1 = countPrefix('N');
data1 = textscan(fid,'%s %s %d %s %s %d %d %f %f %f %[| ]', nEntries1, 'delimiter','|');
skipLines(2)
names2 = getNames;
skipLines(1);
nEntries2 = countPrefix('L');
data2 = textscan(fid,'%s %s %s %s %s %f %d %f %f %f %f %[| ]', nEntries2, 'delimiter','|');
fclose(fid);
getData(data1, names1);
getData(data2, names2);
function names = getNames()
names = fgetl(fid);
names = textscan(names,'%s','delimiter','|');
end
function getData(data, names)
for i = 1:size(data,2)-1
values = ( data{i}(1:end));
if(iscell(values))
values = cell2mat(values);
end
name = names{1}{i+1};
% very basic error checking
if(~strcmp(name, ''))
%save the value in the calling work space
assignin('base', name, values)
end
end
end
function skipLines(n)
while(n > 0)
fgetl(fid);
n = n - 1;
end
end
function n = countPrefix(prefix)
pos = ftell(fid);
n = 0;
currLine = fgetl(fid);
while(currLine(1) == prefix)
currLine = fgetl(fid);
n = n + 1;
end
fseek(fid, pos, 'bof');
end
end
Related
I want to count the permutations with duplicates and only 3 different signs 0,1,2 and the restriction that no 0 follows directly after a 1.
Example:
Valid: 01202212
Invalid: 10...
I though the count of these subset of permutations
is (3^count(0))*(2^count(1))*(3^count(2)) but this is wrong. If not how can i compute the exakt count?
It is clear that:
-valid sequence of length K ending with 1 might be composed
adding 1 to any valid sequence of length K-1
-valid sequence of length K ending with 2 might be composed
adding 2 to any valid sequence of length K-1
-valid sequence of length K ending with 0 might be composed
adding 0 to valid sequences of length K-1 ending with 0 or 2
So simple Python program
def valid123(n):
a = [[0]*3 for _ in range(n)]
a[0][0] = 1
a[0][1] = 1
a[0][2] = 1
summ = 3
for i in range(1, n):
a[i][0] = summ - a[i-1][1]
a[i][1] = summ
a[i][2] = summ
summ = sum(a[i])
return summ
for i in range(1,10):
print(i, valid123(i))
gives
1 3
2 8
3 21
4 55
5 144
6 377
7 987
8 2584
9 6765
Corresponding OEIS sequence has simple recurrent representation a(n) = 3*a(n-1) - a(n-2) - subset of Fibonacci, and some closed formula does exist:
a(n) = (ap^n - am^n)/(ap-am), with ap := (3+sqrt(5))/2, am := (3-sqrt(5))/2
I have a n x m array (could be any size array but it will not be a 1 x m) and I want to rotate / shift each square loop individually no matter the array size.
How can I alternate the rotation / shift each square loop no matter the size of the array.
Please note: I'm not trying to calculate the values in the array but shift the values.
My thought process was to get the values of each "square loop" and place them into one row and do a circshift then place them back into another array.
I ran into problems trying to get the values back into the original n x m array size and I wasn't sure how I could loop through the process for different n x m arrays.
The pink highlighted section, left of the arrows is the starting position of the array and it's "loops" and the green highlighted section, right of the arrows is the type of rotation / shift of the values that I'm trying to create. The array could have more than 3 "loops" this is just an example.
Code below:
I=[1:5;6:10;11:15;16:20;21:25;26:30]
[rw,col] = size(I);
outer_1=[I(1,:),I(2:end-1,end).',I(end,end:-1:1),I(end-1:-1:2,1).'] %get values in one row (so I can shift values)
outer_1_shift=circshift(outer_1,[0 1]) %shift values
new_array=zeros(rw,col);
Ps: I'm using Octave 4.2.2 Ubuntu 18.04
Edit: The circshift function was changed for Octave 5.0, the last edit made it compatible with previous versions
1;
function r = rndtrip (n, m, v)
rv = #(x) x - 2 * (v - 1);
r = [v * ones(1,rv(m)-1) v:n-v+1 (n-v+1)*ones(1,rv(m)-2)];
if (rv(m) > 1)
r = [r n-v+1:-1:v+1];
endif
endfunction
function idx = ring (n, m , v)
if (2*(v-1) > min (n, m))
r = [];
else
r = rndtrip (n, m, v);
c = circshift (rndtrip (m, n, v)(:), - n + 2 * v - 1).';
idx = sub2ind ([n m], r, c);
endif
endfunction
# your I
I = reshape (1:30, 5, 6).';
# positive is clockwise, negative ccw
r = [1 -1 1];
for k = 1:numel(r)
idx = ring (rows(I), columns(I), k);
I(idx) = I(circshift(idx(:), r(k)));
endfor
I
gives
I =
6 1 2 3 4
11 8 9 14 5
16 7 18 19 10
21 12 13 24 15
26 17 22 23 20
27 28 29 30 25
run it on tio
So, I had the same idea as in Andy's comment. Nevertheless, since I was already preparing some code, here is my suggestion:
% Input.
I = reshape(1:30, 5, 6).'
[m, n] = size(I);
% Determine number of loops.
nLoops = min(ceil([m, n] / 2));
% Iterate loops.
for iLoop = 1:nLoops
% Determine number of repetitions per row / column.
row = n - 2 * (iLoop - 1);
col = m - 2 * (iLoop - 1);
% Initialize indices.
idx = [];
% Add top row indices.
idx = [idx, [repelem(iLoop, row).']; iLoop:(n-(iLoop-1))];
% Add right column indices.
idx = [idx, [[iLoop+1:(m-(iLoop-1))]; repelem(n-(iLoop-1), col-1).']];
if (iLoop != m-(iLoop-1))
% Add bottom row indices.
idx = [idx, [repelem(m-(iLoop-1), row-1).'; (n-(iLoop-1)-1:-1:iLoop)]]
end
if (iLoop != n-(iLoop-1))
% Add left column indices.
idx = [idx, [[(m-(iLoop-1))-1:-1:iLoop+1]; repelem(iLoop, col-2).']]
end
% Convert subscript indices to linear indices.
idx = sub2ind(size(I), idx(1, :), idx(2, :));
% Determine direction for circular shift operation.
if (mod(iLoop, 2) == 1)
direction = [0 1];
else
direction = [0 -1];
end
% Replace values in I.
I(idx) = circshift(I(idx), direction);
end
% Output.
I
Unfortunately, I couldn't think of a smarter way to generate the indices, since you need to maintain the right order and avoid double indices. As you can see, I obtain subscript indices with respect to I, since this can be done quite easy using the matrix dimensions and number of loops. Nevertheless, for the circshift operation and later replacing of the values in I, linear indices are more handy, so that's why the sub2ind operation.
Input and output look like this:
I =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
I =
6 1 2 3 4
11 8 9 14 5
16 7 18 19 10
21 12 13 24 15
26 17 22 23 20
27 28 29 30 25
I was right, that the "shift direction" changes with every loop?
Hope that helps!
Caution: I haven't tested for generality, yet. So, please report any errors you might come across.
I have a square matrix B and I want to extract its submatrix which has consecutive row numbers and column numbered 1 through k, with k every natural number no more than n (size of my matrix). It also needs to have non-zero main diagonal entries.
Furthermore, I want to store the submatrices in an array form, (the next step is to check if their determinants are positive, but I won't include that in this question. Here is the code I have built:
for i = 1:n
for j = 1:n-i+1
submat2{i,j} = B([j:j+i-1],[1:i]);
for k = 1:i
maindiag{i,j,k} = prod((submat2{i,j}(i,i) ~= 0));
end
matmaindiag = []
for l = 1:size(maindiag(i,j,:),3)
matmaindiag = [matmaindiag cell2mat(maindiag(i,j,l))]
if prod(matmaindiag ~= 0)
boundsub{end+1} = submat2{i,j};
end
end
end
end
Is there any better way to do this?
For example, if I have:
B =
6 7 8 9
11 12 13 14
0 17 18 19
0 0 23 24
then the submatrices I would like to extract are:
B([1],[1]), B([1],[2]), B([1,2],[1,2]), B([2,3],[1,2]), B([1,2,3],[1,2,3]), B([2,3,4],[1,2,3]), and B itself
since they:
consisted of entries from columns of B that numbered consecutively from 1 through k (the size of the submatrix itself) and
From the consecutively numbered rows of B
Don't have any zero-valued diagonal entries
Thanks for any help and thoughts :)
I have come up with this
n = size(B,1)
for i = 1:n
for j = 1:n-i+1
submat{i,j} = B([j:j+i-1],[1:i]);
end
end
bousub = []
for i = 1:n
for j = 1:n-i+1
dia = diag(submat{i,j});
if (prod(dia) ~= 0)
bousub{end+1} = submat{i,j};
end
end
end
Say I have an array the size 100x150x30, a geographical grid 100x150 with 30 values for each grid point, and want to find consecutive elements along the third dimension with a congruous length of minimum 3.
I would like to find the maximum length of consecutive elements blocks, as well as the number of occurrences.
I have tried this on a simple vector:
var=[20 21 50 70 90 91 92 93];
a=diff(var);
q = diff([0 a 0] == 1);
v = find(q == -1) - find(q == 1);
v = v+1;
v2 = v(v>3);
v3 = max(v2); % maximum length: 4
z = numel(v2); % number: 1
Now I'd like to apply this to the 3rd dimension of my array.
With A being my 100x150x30 array, I've come this far:
aa = diff(A, 1, 3);
b1 = diff((aa == 1),1,3);
b2 = zeros(100,150,1);
qq = cat(3,b2,b1,b2);
But I'm stuck on the next step, which would be: find(qq == -1) - find(qq == 1);. I can't make it work.
Is there a way to put it in a loop, or do I have to find the consecutive values another way?
Thanks for any help!
A = randi(25,100,150,30); %// generate random array
tmpsize = size(A); %// get its size
B = diff(A,1,3); %// difference
v3 = zeros(tmpsize([1 2])); %//initialise
z = zeros(tmpsize([1 2]));
for ii = 1:100 %// double loop over all entries
for jj = 1:150
q = diff([0 squeeze(B(ii,jj,:)).' 0] == 1);%'//
v = find(q == -1) - find(q == 1);
v=v+1;
v2=v(v>3);
try %// if v2 is empty, set to nan
v3(ii,jj)=max(v2);
catch
v3(ii,jj)=nan;
end
z(ii,jj)=numel(v2);
end
end
The above seems to work. It just doubly loops over both dimensions you want to get the difference over.
The part where I think you were stuck was using squeeze to get the vector to put in your variable q.
The try/catch is there solely to prevent empty consecutive arrays in v2 throwing an error in the assignment to v3, since that would remove its entry. Now it simply sets it to nan, though you can switch that to 0 of course.
Here's one vectorized approach -
%// Parameters
[m,n,r] = size(var);
max_occ_thresh = 2 %// Threshold for consecutive occurrences
% Get indices of start and stop of consecutive number islands
df = diff(var,[],3)==1;
A = reshape(df,[],size(df,3));
dfA = diff([zeros(size(A,1),1) A zeros(size(A,1),1)],[],2).'; %//'
[R1,C1] = find(dfA==1);
[R2,C2] = find(dfA==-1);
%// Get interval lengths
interval_lens = R2 - R1+1;
%// Get max consecutive occurrences across dim-3
max_len = zeros(m,n);
maxIDs = accumarray(C1,interval_lens,[],#max);
max_len(1:numel(maxIDs)) = maxIDs
%// Get number of consecutive occurrences that are a bove max_occ_thresh
num_occ = zeros(m,n);
counts = accumarray(C1,interval_lens>max_occ_thresh);
num_occ(1:numel(counts)) = counts
Sample run -
var(:,:,1) =
2 3 1 4 1
1 4 1 5 2
var(:,:,2) =
2 2 3 1 2
1 3 5 1 4
var(:,:,3) =
5 2 4 1 2
1 5 1 5 1
var(:,:,4) =
3 5 5 1 5
5 1 3 4 3
var(:,:,5) =
5 5 4 4 4
3 4 5 2 2
var(:,:,6) =
3 4 4 5 3
2 5 4 2 2
max_occ_thresh =
2
max_len =
0 0 3 2 2
0 2 0 0 0
num_occ =
0 0 1 0 0
0 0 0 0 0
I have this data file, for Matlab:
A 12 E 24
B 34 F 67
C 23 G 88
D 56 H 33
I would like to use a for loop to find the sum of the numbers in the 2nd and 4th column. I would really appreciate the help. Thank you.
This is what I have so far
`fid = fopen('patwts.dat')
if fid == -1
disp ('File open not successful')
else
disp ('File open is successful')
mat = textscan(fid,'%c %f %c %f')
[r c] = size(mat)
for i = 1:r
for j = 1:c
sum(j)
end
end
fclose(fid)
end`