Read from .txt file a sparse representation of matrix - sparse-matrix

I have a .txt file of the form:
0 1 10 100 130 140
1 2 12 67 190
1 3 101 788 900
where the first column represents sentiments and the rest of the columns represent the feature that is 1. How can I convert this sparse data into a full matrix with all the features as columns and 0/1 values?

Related

How is an array sliced?

I have some sample code where the array is sliced as follows:
A = X(:,2:300)
What does this mean about the slice of the array?
: stands for 'all' if used by itself and 2:300 gives an array of integers from 2 to 300 with a spacing of 1 (1 is implicit) in MATLAB. 2:300 is the same as 2:1:300 and you can even use any spacing you wish, for example 2:37:300 (result: [2 39 76 113 150 187 224 261 298]) to generate equally spaced numbers.
Your statement says - select every row of the matrix A and columns 2 to 300. Suggested reading

comparing multiple column files using python3

input_file1:
a 1 33
a 34 67
a 68 78
b 1 99
b 100 140
c 1 70
c 71 100
c 101 190
input file2:
a 5 23
a 30 72
a 76 78
b 5 30
c 23 88
c 92 98
I want to compare these two files such that for every value of 'a' in file2 the two integers (boundary) fall in the range (boundaries) of 'a' in file1 or between two ranges.
Instead of storing values like this 'a 1 33', you can make one structure (like 'a:1:33') for your data while writing into file. So that it will become easy to read data also.
Then, you can read each line and can split it based on ':' separator and you can compare with another file easily.

Total values of makes some litter difference how can i get exact value in sql?

Speed A b c Total
129 11 4 2.75 354.75
91 9 6 1.5 136.5
166 19 8 2.375 395.08
164 26 12 2.16666666666667 355.88
146 11 6 1.83333333333333 267.18
147 16 8 2 294
201 8 3 2.66666666666667 536.67
164 4 2 2 328
186 8 6 1.33333333333333 247.38
165 7 4 1.75 288.75
171 10 4 2.5 427.5
104 5 4 1.25 130
1834 134 67 2 3668
Iam using total=Speed*c
But the value is not getting equal if i add all values for last column 1834 .I am getting 100 ampunt less.
The first:
Total = Speed * c, this expression about your output I suppose is rounded at the second number after comma.
The second:
You have put 2 as value of c in the last row. Why? This is the average of all previous rows?
So, if you take the forst and the second, you'll have cause about you have a difference with the last row and the other rows
Approximate-number data types for use with floating point numeric data.
Floating point data is approximate; therefore, not all values in the data
type range can be represented exactly.
Float has a default 15 digit precision. So when your number has more digits it will round off the rest of the digits.
You are showing total with only 2 decimals. You could consider permanently changing c to decimal(20,14). Here is the result if you convert it to decimal, this should be more exact:
select cast(c as decimal(20, 14)) * speed as Total
from table

Matlab populate arrays of matrices

I am still a newbe and I have probabily a very easy question concerning arrays of matrices. I have a matrix of nrows like the following one:
>> matrix
1 678 543
2 676 541
3 543 987
4 543 98
1 433 54
2 908 32
3 457 54
4 235 21
How to create arrays of equal size matrices?
i.e array{i,1}
This is replication of question:
Array of Matrices in MATLAB
and probably of many others.
What is not clear to me, is how to populate my array of fixed dimension matrices. So that
>>array{1,1}
1 678 543
2 676 541
3 543 987
4 543 98
Here is my attempt:
Find all the ones in column 1 of matrix and the size of matrix.
Create cell arrays, look in each line, if it is equal to 1 create an array{i,1} of zeros equal to the size of the matrices I want to create (in my case 4x3).
If not equal to 1 insert into the array the first four values of matrix.
Is there any faster way to do it without a loop?
You can also use mat2cell:
mat2cell(matrix, [4 4])

How to save data in .txt file in MATLAB

I have 3 txt files s1.txt, s2.txt, s3.txt.Each have the same format and number of data.I want to combine only the second column of each of the 3 files into one file. Before I combine the data, I sorted it according to the 1st column:
UnSorted file:
s1.txt s2.txt s3.txt
1 23 2 33 3 22
4 32 4 32 2 11
5 22 1 10 5 28
2 55 8 11 7 11
Sorted file:
s1.txt s2.txt s3.txt
1 23 1 10 2 11
2 55 2 33 3 22
4 32 4 32 5 28
5 22 8 11 7 11
Here is the code I have so far:
BaseFile ='s'
n=3
fid=fopen('RT.txt','w');
for i=1:n
%Open each file consecutively
d(i)=fopen([BaseFile num2str(i)'.txt']);
%read data from file
A=textscan(d(i),'%f%f')
a=A{1}
b=A{2}
ab=[a,b];
%sort the data according to the 1st column
B=sortrows(ab,1);
%delete the 1st column after being sorted
B(:,1)=[]
%write to a new file
fprintf(fid,'%d\n',B');
%close (d(i));
end
fclose(fid);
How can I get the output in the new txt file in this format?
23 10 11
55 33 22
32 32 28
22 11 11
instead of this format?
23
55
32
22
10
33
32
11
11
22
28
11
Create the output matrix first, then write it to the file.
Here is the new code:
BaseFile ='s';
n=3;
for i=1:n % it's not recommended to use i or j as variables, since they used in complex math, but I'll leave it up to you
% Open each file consecutively
d=fopen([BaseFile num2str(i) '.txt']);
% read data from file
A=textscan(d,'%f%f', 'CollectOutput',1);
% sort the data according to the 1st column
B=sortrows(A{:},1);
% Instead of deleting a column create new matrix
if(i==1)
C = zeros(size(B,1),n);
end
% Check input file and save the 2nd column
if size(B,1) ~= size(C,1)
error('Input files have different number of rows');
end
C(:,i) = B(:,2);
% don't write yet
fclose (d);
end
% write to a new file
fid=fopen('RT.txt','w');
for k=1:size(C,1)
fprintf(fid, [repmat('%d\t',1,n-1) '%d\n'], C(k,:));
end
fclose(fid);
EDIT:
Actually to write only numbers to a file you don't need FPRINTF. Use DLMWRITE instead:
dlmwrite('RT.txt',C,'\t')

Resources