error reading a text file in octave - matlab

I have a text file named xMat.txt which has 200 space separated elements in one line and some 767 lines.
This is how xMat.txt looks.
386.0 386.0 388.0 394.0 402.0 413.0 ... .0 800.0 799.0 796
801.0 799.0 799.0 802.0 802.0 80 ... 399.0 397.0 394.0 391
.
.
.
When I try to read the file in octave using X = dlmread('xMat.txt',' ') I get a matrix of size 767 X 610. I am expecting a matrix of size 767 X 200 since there are 200 elements in one row. How can I solve this problem?
Edit - This is my file

Your uploaded file https://bpaste.net/raw/96cf21aa21b8 has incosistent number of columns per row.
$ awk "{print NF}" tmp | sort | uniq -c
2 200
754 201
1 206
1 217
1 223
1 234
1 237
1 238
1 269
1 273
1 390
1 420
1 610
So the most rows have 201 columns but one has 420 columns and one even has 610 columns. This is the reason you get a 767x610 matrix from dlmread.
Lets look which lines have more than 201 columns:
$ awk "{if (NF>201) print NR, NF}" tmp
68 217
580 206
613 390
615 234
657 273
676 610
679 237
720 269
722 238
743 223
762 420
The first coloumn shows the line number, the second number of columns.
So your line with 610 columns is line number 676. I aslo printed line 676:
so you see it really contains data, no multiple spaces which are filles with zeros.

Related

tesseract OCR output words bounds

How to output words bounds using tesseract command line with config file?
So far I been able to output chars using
tesseract image.png myBox makebox
This created a myBox.box file that looks like this:
N 51 1844 75 1874 0
o 80 1843 100 1867 0
S 113 1843 136 1875 0
I 140 1844 145 1874 0
M 151 1844 181 1874 0
c 197 1843 216 1867 0
a 219 1843 238 1867 0
r 243 1844 254 1867 0
d 256 1843 275 1876 0
How ever those only chars and I need words, so I been able to combine it with standard output
tesseract image.png myBox
This creates a file like this:
no simcard
Combining those two outputs I can get words bounds. How ever I prefer to find a method that does not require examining the same image twice. Please help

MATLAB accessing conditional values and performing operation in single column

Just started MATLAB 2 days ago and I can't figure out a non-loop method (since I read they were slow/inefficient and MATLAB has better alternatives) to perform a simple task.
I have a matrix of 5 columns and 270 rows. What I want to do is:
if the value of an element in column 5 of matrix goodM is below 90, I want to take that element and and subtract it from 90.
So far I tried:
test = goodM(:,5) <= 90;
goodM(test) = 999;
It changes all goodM values within column 1 not 5 into 999, in addition this method doesn't allow me to perform operations on the elements below 90 in column 5. Any elegant solution to doing this?
edit:: goodM(:,5)(test) = 999; doesn't seem to work either so I have no idea to specify the target column.
I am assuming you are looking to operate on elements that have values below 90 as your text in the question reads, rather than 'below or equal to' as represented by '<=' as used in your code. So try this -
ind = find(goodM(:,5) < 90) %// Find indices in column 5 that have values less than 90
goodM(ind,5) = 90 - goodM(ind,5) %// Operate on those elements using indices obtained from previous step
Try this code:
b=90-a(a(:,5)<90,5);
For example:
a =
265 104 479 13 176
26 110 447 208 144
379 163 179 366 464
301 48 274 391 26
429 374 174 184 297
495 375 312 373 82
465 272 399 447 420
205 170 373 122 84
1 417 63 65 252
271 277 412 113 500
then,
b=90-a(a(:,5)<90,5);
b =
64
8
6

How two reshape two columns into multiple columns in matlab

Does anyone know how I can reshape these two columns:
1 1
1 1
1 1
379 346
352 363
330 371
309 379
291 391
271 402
268 403
1 1
1 1
406 318
379 334
351 351
329 359
307 367
287 378
267 390
264 391
into these four columns:
1 1 1 1
1 1 1 1
1 1 406 318
379 346 379 304
352 363 351 351
330 371 329 359
309 379 307 367
291 391 287 378
271 402 267 390
268 403 264 391
That is, how to reshape a matrix that is the size of Nx2 into a size 10xM in matlab?
One solution using mat2cell, splitting every 10 rows. Probably easier to understand, because no 3d-matrices are used:
cell2mat(mat2cell(x,repmat(10,size(x,1)/10,1),size(x,2))')
Second solution using reshape and permute, should be faster but I did not try it.:
reshape(permute(reshape(x,10,[],size(x,2)),[1,3,2]),10,[])

Find a value in a column and output a variable from the same row in Matlab

Find a value in a column and output a variable from the same row.
I have a dataset in Matlab which looks like this:
423 2433 443
424 4333 233
425 3422 334
426 3241 213
427 2342 234
I need to make it such that if I use "425", and search for the corresponding values in the row that has "425"... the code would pull 3422 and 334 respectively.
What code should I use to do this in Matlab?
Help would be appreciated,
Sincerely
Ben
Easy with logical indexing:
data = [
423 2433 443
424 4333 233
425 3422 334
426 3241 213
427 2342 234 ];
result = data(data(:,1)==425,2:end)
gives you a vector with the two desired numbers.
result = data(data(:,1)==425,2:end)

MATLAB: Remove n last rows from matrix without loop

I have a problem with removing surplus rows in the end of the matrix. In general, I need to remove rows that contain a specific elements in a specific column, without using a loop. It seems easy but I still keep on getting some weird outcomes.
For simple example, let's have a 10x10 matrix A:
A=[1:10; 901:910; 201:210; 301:310; 701:710; 401:410; 601:610; 501:510; 801:810; 101:110];
And I want to remove (for better illustration just replace with ones) that rows from fourth to the last, whose third columns contain value higher than 600. Result should look like that:
| 1 2 3 4 5 6 7 8 9 10|
|901 902 903 904 905 906 907 908 909 910|
|201 202 203 204 205 206 207 208 209 210|
|301 302 303 304 305 306 307 308 309 310|
| 1 1 1 1 1 1 1 1 1 1|
|401 402 403 404 405 406 407 408 409 410|
| 1 1 1 1 1 1 1 1 1 1|
|501 502 503 504 505 506 507 508 509 510|
| 1 1 1 1 1 1 1 1 1 1|
|101 102 103 104 105 106 107 108 109 110|
My idea looks like that:
A(A(4:end,3)>600,:)=[1];
But the outcome is some nonsense matrix.
Thanks for your help!
A([false(3,1);A(4:end,3)>600],:)=1;
and as #yoda said, to remove the rows do:
A([false(3,1);A(4:end,3)>600],:)=[];
Firstly your problem is that entering a matrix inside a matrix means use the values of the matrix as indices. since your matrix only has zeros and ones (as a result of the "<" operator) you got a weird result.
To answer your question, you need to replace the matrix with a smaller matrix. something like:
A = new_matrix
new_matrix is generated out of A by taking only the rows you want.
new_matrix = A(row_indices, :)
Where row_indices is the vector of indices you want to keep. To build that you can start with a zeros-ones vector and apply find (which will yield the indices with ones). So we want:
row_indices = find([1,1,1, A(4:end,3).'<600])
The first 3 ones are because you always want the first 3 lines.
So putting everything together gives
A = A(find([1,1,1,A(4:end,3).'<600]),:)
Running this on your example values :
>> A=[1:10; 901:910; 201:210; 301:310; 701:710; 401:410; 601:610; 501:510; 801:810; 101:110];
>> A(find([1,1,1,A(4:end,3).'<600]),:)
ans =
1 2 3 4 5 6 7 8 9 10
901 902 903 904 905 906 907 908 909 910
201 202 203 204 205 206 207 208 209 210
301 302 303 304 305 306 307 308 309 310
401 402 403 404 405 406 407 408 409 410
501 502 503 504 505 506 507 508 509 510
101 102 103 104 105 106 107 108 109 110
BTW, changing these rows to ones is just as easy:
A(find([0,0,0,A(4:end,3).'>600]),:) = 1