Posts

Showing posts with the label matlab

How to impliment logical indexing faster in matlab? [closed]

How to impliment logical indexing faster in matlab? [closed] I have several matrices that I need to add to one large matrix. The large matrix (300002x50) is split up by .001 seconds and the timing for the other 49 matricies (14250x2) are roughly .02 apart, but not uniformly distributed. I have tried find to index the entries from the smaller matrices into the larger matrix, but it was too slow. I have since tried: find for a = 1:length(test) aaa = abs(AF1(:,1)-test(a,1))<10^-6; AF1(aaa,index)=test(a,2); end Where test is a 14250x2 double (time,data) , AF1 is a 300002x50 double matrix and index is which column in AF1 the data will be added to. It was a bit faster, but it still takes up 99.3% (29 minutes) of the time. It works how I want it, but is there any way to implement this in a faster manner? test (time,data) AF1 index AF1 Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. A...

Why is my logical mask not working on a 2D matrix in matlab properly?

Why is my logical mask not working on a 2D matrix in matlab properly? X(100,371) %% contains 100 datapoints for 371 variables I want to keep only the data which are within mean+standard deviation:mean-standard deviation. This is how I am proceeding: mx=mean(X);sx=std(X); %%generate mean, std %%this generates mx(1,371) and sx(1,371) mx=double(repmat(mx,100,1)); %%this fills a matrix with the same datapoints, %%100 times sx=double(repmat(sx,100,1)); %% this gives mx(100,371) and sx(100,371) g=X>mx-sx & X<mx+sx; %%this creates a logical mask g(100,371) %%filled with 1s and 0s test(g)=X(g); %%this should give me test(100,371), but I get %%test(37100), which is wrong as it doesnt maintain %%the shape of X test=reshape(test,100,371) %% but when I compare this to the my original matrix %% X(100,371) I hardly see a difference (datapoints %% in test are still outside the range I want. What am I doing wrong? 1 Answer ...

Logical arrays as indices to arrays to convert elements to zero in MATLAB

Logical arrays as indices to arrays to convert elements to zero in MATLAB I am trying to convert some elements of a matrix to zeros depending on a logical array. Suppose we have a random 5 x 5 matrix: 5 x 5 b = 0.0344 0.1869 0.7547 0.1190 0.2238 0.4387 0.4898 0.2760 0.4984 0.7513 0.3816 0.4456 0.6797 0.9597 0.2551 0.7655 0.6463 0.6551 0.3404 0.5060 0.7952 0.7094 0.1626 0.5853 0.6991 And I have an array of zeros and ones with the same dimension: a = 0 1 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 1 1 0 Doing a(logical(b)) gives me the elements I am looking for, but in a vector form: a(logical(b)) ans = 0.4387 0.3816 0.1869 0.4456 0.6463 0.7547 0.2760 0.6551 0.1626 0.4984 0.9597 0.5853 0.2551 0.5060 How can I get the following matrix instead? 0 0.1869 0.7547 0 0 0.4387 0 0.2760 0.4984 0.7513 0.3816 0.4456 ...

How to make empty lists in MATLAB Live Script and append to them both outside of a loop and inside one? [closed]

How to make empty lists in MATLAB Live Script and append to them both outside of a loop and inside one? [closed] The three list I want to make are the following and I'm not sure how it's done in MATLAB. na = nb = t Outside of the list I want to append the follwoing: NA = input('Enter intial number of atoms of A > '); NB = input('Enter intial number of atoms of B > '); tau_a = input('Define decay constant of A >'); tau_b = input('Define decay constant of B >'); time = input('the total time >'); dt = input('Define time increment >'); I've to append the values like so: na = [na, NA] nb = [nb; NB] t = [t, 0] Finally, inside the loop I have tried appending values like this: for i = int16(time/dt) nai = na[i]-(na[i]/tau_a)*dt nbi = nb[i]+((na[i]/tau_a)-(nb[i]/tau_b))*dt ti = t[i]+dt na = [na, nai] nb = [nb, nbi] t = [t, ti] end But it is not working. It does not want to append the values. ...

How to do a code optimization for data export/import to uielements

How to do a code optimization for data export/import to uielements I have a gui with a uitable with a maximum of 30 rows and 30 edit-boxes. I what 2 specific things. 1) Export the cell array with the uitable strings (4 columns and 1–30 rows). Then I want the values of the 30 edit-boxes inserted in the cell array in column 5. 2) Finally I want to be able to load the exported cell array; the 4 first columns into the uitable and the 5th column into the edit-boxes. The code I have created (see below) is exporting/importing an excel sheet but it is very slow, even with only 5–7 rows in the uitable. How can I make this faster and more elegant. I doesn’t have to be an excel sheet. It can be a *.txt, *.cvs or something else (not *.mat). % --- Dataset example GUIstate = { 'REF20171002-2.txt' 'REF20171002-2' '217096907-1.txt' [217096907] [1] 'REF20171002-4.txt' 'REF20171002-4' '217096909-1.txt' [217096909] [3] ...

MATLAB load not working with variable filename [duplicate]

MATLAB load not working with variable filename [duplicate] This question already has an answer here: I try to load a .mat file in MATLAB R2016a. However, when I make the filename variable, it fails with Error using load Unable to read file 'filename'. No such file or directory. The documentation for R2018a states that the filename has to be specified as a character vector or string which i did. I searched for similar questions on SO, but they were all due to typing errors, e.g. Error using load; Unable to read file matlab Code for reproduction: clear all mat1 = magic(5); save mat1 clear mat1 load mat1 % working clear mat1 filename = 'mat1.mat'; % tried with/without .mat load filename % not working This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. Could the down-voters please give a reason? It took me some time to figure this out; it was not answered...