ript it as a regular array. To preempt objections: a) Yes, I could write a class to do it but that’s not very fast. b) The object could be read only, so that you wouldn’t have to worry about multiple numerical indices with the same value.
Thank you very much for Method #4.
I’ve been trying hard to find a way for vectorizing relational matrix manipulation in Matlab.
Maybe there is an even simpler way that I am not aware of, but your #4 works.
I used it with
indices = sub2ind(size(A),rnotA+1,cnotA+1);
Note +1. I basically needed to manipulate the i+1,j+1 neighbors of elements satisfying a condition without relying on a for loop.
i want to calculate numbers of columns in a matrix on matlab, will some1 kindly help me,,,?
Perhaps the function size will help you out, especially with 2 inputs ( http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html ).
hi i would like to ask something…I want to remove an especific NaN element from a matrix, I do not know how to do it. All the solutions that I’ve read explain how to remove rows where the NaN exists, but not specific elements..Could you please help me? I appreciate your help
If you have a vector, you can remove NaNs like so:
a(isnan(a)) = ;
But if you have a matrix, you aren’t guaranteed that the same number of NaNs are in each row/column so you may not end up with a rectangular array.
i have a question about removing elements from an array based on their indices, not their values. please consider the following code snippet showing 3 methods for doing such an operation:
n=100; % square nxn matrices n2=n^2; A0=rand(n,n,n); % generate matrices for i=1:n % for each matrix % method 1 of dropping a matrix A1=A0; A1(:,:,i)=; % method 2 of dropping a matrix A2=A0; A2(1:n2)=; % method 3 of dropping a matrix A3=A0(:,:,2:n); % mean computation for comparison purposes mean(A0,3); end
note that upon running profiler, method 3 takes about half the time of method 1, and about 1/3 of the time of method 2. but the mean computation takes far less time than any!
this quite surprising result (to me) leads me to have the following questions:
1) why does it take so long?
2) is there a way to make it go *much* faster?
mean takes less time since there’s no need to copy the array at all before acting on it.
What exactly is taking so long that you want to go faster? I doubt you can do better than the 3rd method, which I think makes the fewest temporary intermediate arrays (none). It only makes a copy of the array to keep and nothing more.
hey loren, thanks for the response. so, the first line of the first two methods, where the copy is made, takes nearly no time. it is the second line, where i only discard elements of the matrix, that takes time. that might have been confusing. in practice, what i do now is something like:
where ‘s’ is a vector listing the indices that i care about.
and the reason i care about all this, is because i am doing leave-one-out cross-validation. i have a bunch of training data, and i am fitting my model, but want to ensure that i have not overfit. so, i loop through every data point, discard it to fit the model, and then check the model accuracy on it. the part of the code that takes by far the longest was dropping a matrix from the array. now, i skip that by doing it all in one line, as shown above. but now that line just takes about as long as dropping the matrix did, so i didn’t get any speed up.
specifically, my code now looks like this:
n=100; % square nxn matrices n2=n^2; A0=rand(n,n,n); % generate matrices for i=1:n % for each matrix % method 1 of getting mean A1=A0(:,:,2:n); M1=mean(A3,3); % method 2 of getting mean M2=mean(A0(:,:,2:n),3); % mean computation for comparison purposes end
running the profiler shows that the line to compute M2 takes equally long as the line to compute A1 and M1 combined. and, if i look carefully, i see that actually computing M2, matlab only spends a fraction of the time computing the mean, the rest is passing the variable to the mean function.
so my question remains: is there anyway for matlab to not take a long time when drop