MATLAB vectorize nested for loop - matlab

Hi I am struggling with matlab vactorization any help on this would be great thanks.
oldLocation, Limit_ are both matrices
for i=1:250
for j=1:350
temp= oldLocation(i,j,:)./Limit_(i,j,:);
end
end

Just perform the division directly. Of course, you'll need to adapt the code that follows (unless you keep overwriting temp)
temp = oldLocation./Limit_;

Related

Permuation in a loop on Matlab

I am trying to write a code that reshapes my data (35x2) 10000 times. Here is my code for this
N = 35;
reshape(table1(randperm(2*N)),N,2);
In each iteration (there is going to be 10000) the mean difference between the randomly generated groups is needed. However, I couldn't figure out a way to do so.
for i = 1:10000
permutatedvers(i) = reshape(i)(table1(randperm(2*N)),N,2);
end
So I tried these kinds of codes but I am getting errors. How can I implement this in a loop?
Ok, so I found a solution and want to share with you just in case anyone needs it in the future.
Rather than using a for loop, I decided to go with while loop.
N=length(table1)
ii=1;
while (ii<10001)
t1perm= reshape(table1(randperm(2*N)),N,2);
t1permA=t1perm(:,1); %groupA
t1permB=t1perm(:,2); %groupB
meandif(ii)= mean(t1permA)-mean(t1permB); %meandif gives us a 1x10000 matrix
ii= ii+1;
end
Feel free to criticize and update my code. Thanks!

How can I speed up this loop?

I have a huge matrix (5000x5000x100) and I'm trying to smooth each index along the third dimension, but it takes HOURS. I am doing something inefficiently.
new_mat=zeros(size(my_mat));
for i = 1:length(mymat)
for j = 1:length(mymat)
new_mat(i,j,:) = wdenoise(squeeze(mymat(i,j,:)));
end
end
I know arrays and indexing would help but I'm not sure how to apply them here. Thanks for any help.
I don't have access to the newer Wavelet Toolbox needed for wdenoise, but since the function will operate across columns if you provide a matrix you should be able to remove the inner loop which may increase the speed a bit:
new_mat=zeros(my_mat)
for i = 1:length(mymat)
new_mat(i,:,:) = wdenoise(squeeze(mymat(i,:,:)));
end

Optimize nested for loops in Matlab using Vectorization

I am a newbie to Matlab and I am currently trying to optimize a nested for loop as below. The loop is currently running forever for my input.
for i = 1:size(mat,1)
for j = 1:size(mat,2)
mat(i,j) = some_mapping(mat(i,j)+1);
end
end
However I can't find a way to vectorize it. I have tried bsxfun and arrayfun but it does not seem to work (or even run more slowly than the loop).
Maybe I was doing it in a wrong way. Any help is appreciated!
As suggested by Andras Deak, if some_mapping is simply a look-up-table operation, then
mat = some_mapping( mat+1 );
Notes:
- In order of the mapping to work, the values of mat must be integers in the range [0..numel(some_mapping)-1].
- The size of some_mapping does not affect the size of the result, it will be identical in size to mat.

Too many output arguments

I am not a very hardcore coder in MATLAB, i have learned every thing from youtube and books. This might be a very simple question but i do not know what search for answer.
In MATLAB i am trying to do something like this.
>>[a,b,c] = [1,2,3]
and i want output like this.
>> a = 1
b = 2
c = 3
So Bsically question is that : - User will define the matrix of variables([a,b,c]) in staring of code and during process of the code similar matrix will be displayed and as input a matrix will be asked([1,2,3]). I dont know how do this without writing a loop code in which i will take every single variable from variable matrix and save the value in that variable by eval function.
well above written code is wrong and i know, i can do this with "for" loop and "eval" function.
but problem is that no. of variables(a,b,c) will never be constant and i want know if there exist any in built function or method in MATLAB which will work better than a for loop.
As i told earlier i don't know what to search for such a problem and either this is a very common question.
Either way i will be happy if you can at least tell me what to search or redirect me to a related question.
Please do write if you want any more information or for any correction.
Thank you.
The deal function can do this for a fixed number of inputs:
[A,B,C]=deal(1,2,3)
If you don't know how many inputs you will get beforehand, you have to do some fooling around. This is what I've come up with:
V=[1,2,3,4,5,6,7]
if length(V)>1
for i=1:length(V)
S{i}=['A' num2str(i)];
G{i}=['V(' num2str(i) ')'];
end
T=[S{1} ','];
R=[G{1} ','];
for i=2:length(V)-1
T=[T S{i} ','];
R=[R G{i} ','];
end
T=[T S{length(V)}];
R=[R G{length(V)}];
eval(['[' T ']=deal(' R ')'])
else
A1=V
end
But then dealing with A1, ... , An when you don't know how many there are will be a pain!
This is somehow known as "tuple unpacking" (at least it's what I would search in python!). I could find this thread which explains that you could do this in Octave (I checked and it works in Matlab also). You have to transform the vector into a cell array before:
values = num2cell([1,2,3])
[a,b,c] = values{:}

Matlab -Comparing Values in a Matrix Without using loops in Matlab

As you know, using loops with big size matrix results delay, in my case I need to free my code of loops for the same reason(delay). This is my code example:
for i=1:n
for j=1:m-1
Z=A(i,j);
Z2= A(i,j+1);
if X(Z,Z) == X(Z2,Z)
X(Z2,Z) = X(Z2,Z)+1;
end
end
end
So, are there any suggestion? Many thanks.