1
votes

so the idea here is to read data into a matrix, and then make that matrix a square matrix by removing the excess column(s) or row(s), but vectorized, without using any loops at all and without using if/else. is this possible?

this is the way i have it done with loop and if/else

load file.dat
r = num2str(size('file',1))
c = num2str(size('file',2))
while (r~=c) 
    if (r>c)
        r--
    else (c>r)
        c--
end
res = file(1:r,1:c)
save('squarefile.dat', 'res', '-ascii')

thank you

1
Sorry - but that must be one of the most inefficient algorithm to calculate a minimum. - bdecaf
@bdecaf - I'm working on another algorithm for computing minimum with O( 2^n ) ;-) - Shai
That is not valid matlab syntax. I guess you mean r = r-1 and c = c-1 instead of r-- and c-- - Dennis Jaheruddin
Also the use of num2str does not make sense so just skip that part. - Dennis Jaheruddin

1 Answers

4
votes

Given a matrix A that is nxm big, we'll create matrix B that is a square matrix, by removing excess columns or rows (depending which dimension is bigger) from A:

B=A(1:min(size(A)),1:min(size(A)));

by the way, this line can be also written as A=A(1:min(size(A)),...) if you want to overwrite A and delete the excess info...