rising 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 dropping elements in an array? trying to trick it by putting it all on one line did not seem to work, sadly ;(
The first line takes no time since the 2 arrays are identical and MATLAB only does lazy copy or copy on write (see blogs on memory to learn more about this). Once an element is changing, MATLAB makes a full copy of the original array to be sure it doesn’t get modified improperly.
Again, the best way to drop elements is to create the new array and fill it, you’re 3rd way – only the copy needed for the output is created in addition to the original, I believe. I don’t think you can do better than that.
interesting. ok, i guess i’ll have to live with that ;)
thanks for your help…
hi every body
How to find the position of a matrix element…
Check out the functions find and ismember.
i have red this post, but didn’t find what I was looking for. Is there a easy way to make a logical matrix from subscript indices like the function find does but the other way round. Under method number 2 in your post you gave a small example what happens when you use subscripts to address the selected elements of a matrix. You get the full matrix back and not only the selected elements. I think the easiest way would be a logical to adress and get back only the selected elements. Using the function sub2ind is too much bother in my opinion.
As far as I know, there is not a simple 1-liner to go from subscripts to logicals without using sub2ind. Sorry you don’t care for that solution.
wasnt aware of this before, solved a bug i had been working on for two days already.