Dimension Mismatch MatLab; cant figure out why it is mismatching - matlab

I think it may be a syntax issue, here is the code.
load ([ 'C:\Users\Abid\Desktop\Inquiry Runs\dtS' ])
dtS((dtS==0))=nan;
for j=2:9;
maxS=max(dtS(j,:));
minS=min(dtS(j,:));
maxval(j,:)=dtS((dtS>(maxS-.1*maxS)));
minval(j,:)=dtS((dtS<(minS+.1*minS)));
avmax(j)=mean(maxval(j,:));
avmin(j)=mean(minval(j,:));
avs(j,:)=[avmax(j) avmin(j)]
end
So I know the the row matrices are different sizes every loop. For example maxval(j,:) will change depending one row it is looking through for certain values.
I did this manually and I see that on the first loop the matrices are size (1,1), however, if I set the loop to run for only j=2, the row length is 13.
Usually if a matrix is changing size on the loop, I only get a warning, but this time I think it is due to a reason I don't understand.

You are right that the problem is with maxval(j, :) being a different size. length(maxval(j, :)) is not length(dtS((dtS>(maxS-.1*maxS)))); this causes a problem since maxval has to be 'rectangular', but if it were extended by this line of code, some of its values would not be defined. Consider:
x = [1, 2; 3, 4];
x(3, :) = [5, 6, 7];
If this code were legal, the result would be:
x: [1, 2, ?;
3, 4, ?;
5, 6, 7]
and because of those undefined values, matlab will not let you do this. You could use a cell array instead:
maxval = cell(9, 1);
avmax = zeros(9, 1);
avs = zeros(9, 2);
for j=2:9;
maxS=max(dtS(j,:));
minS=min(dtS(j,:));
maxval{j} = dtS((dtS>(maxS-.1*maxS)));
minval{j} = dtS((dtS<(minS+.1*minS)));
avmax(j)=mean(maxval{j});
avmin(j)=mean(minval{j});
avs(j,:)=[avmax(j) avmin(j)]
end

Related

How to vectorize Matlab Code with mvnpdf in?

I have some working code in matlab, and speed is vital. I have vectorized/optimized many parts of it, and the profiler now tells me that the most time is spent a short piece of code. For this,
I have some parameter sets for a multi-variate normal
distribution.
I then have to get the value from the corresponding PDF at some point
pos,
and multiply it by some other value stored in a vector.
I have produced a minimal working example below:
num_params = 1000;
prob_dist_params = repmat({ [1, 2], [10, 1; 1, 5] }, num_params, 1);
saved_nu = rand( num_params, 1 );
saved_pos = rand( num_params, 2 );
saved_total = 0;
tic()
for param_counter = 1:size(prob_dist_params)
% Evaluate the PDF at specified points
pdf_vals = mvnpdf( saved_pos(param_counter,:), prob_dist_params{param_counter,1}, prob_dist_params{param_counter, 2} );
saved_total = saved_total + saved_nu(param_counter)*pdf_vals;
end % End of looping over parameters
toc()
I am aware that prob_dist_params are all the same in this case, but in my code we have each element of this different depending on a few things upstream. I call this particular piece of code many tens of thousands of time in my full program, so am wondering if there is anything at all I can do to vectorize this loop, or failing that, speed it up at all? I do not know how to do so with the inclusion of a mvnpdf() function.
Yes you can, however, I don't think it will give you a huge performance boost. You will have to reshape your mu's and sigma's.
Checking the doc of mvnpdf(X,mu,sigma), you see that you will have to provide X and mu as n-by-d numeric matrix and sigma as d-by-d-by-n.
In your case, d is 2 and n is 1000. You have to split the cell array in two matrices, and reshape as follows:
prob_dist_mu = cell2mat(prob_dist_params(:,1));
prob_dist_sigma = cell2mat(permute(prob_dist_params(:,2),[3 2 1]));
With permute, I make the first dimension of the cell array the third dimension, so cell2mat will result in a 2-by-2-by-1000 matrix. Alternatively you can define them as follows,
prob_dist_mu = repmat([1 2], [num_params 1]);
prob_dist_sigma = repmat([10, 1; 1, 5], [1 1 num_params]);
Now call mvnpdf with
pdf_vals = mvnpdf(saved_pos, prob_dist_mu, prob_dist_sigma);
saved_total = saved_nu.'*pdf_vals; % simple dot product

Repeating rows of matrix in MATLAB

I have a question that is related to this post: "Cloning" row or column vectors. I tried to work around the answers posted there, yet failed to apply them to my problem.
In my case, I'd like to "clone" each row row of a matrix by converting a matrix like
A = [1,2; 3, 4; 5, 6]
into the matrix
B = [1, 2
1, 2
3, 4
3, 4
5, 6
5, 6]
by repeating each row of A a number of times.
So far, I was able to work with repmat for a single row like
A = [1, 2];
B = repmat(A, 2, 1)
>> B = [1, 2
1, 2]
I was trying to build a loop using that formula, in order to obtain the matrix wanted. The loop looked like
T = 3; N = 2;
for t = 1:T
for I = 1:N
B = repmat(C, 21, 1)
end
end
Has anyone an idea how to correctly write the loop, or a better way to do this?
kron
There are a few ways you can do this. The shortest way would be to use the kron function as suggested by Adiel in the comments.
A = [1,2; 3, 4; 5, 6];
B = kron(A, [1;1]);
Note that the number of elements in the ones vector controls how many times each row is duplicated. For n times, use kron(A, ones(n,1)).
kron calculates the kronecker tensor product, which is not necessarily a fast process, nor is it intuitive to understand, but it does give the right result!
reshape and repmat
A more understandable process might involve a combination of reshape and repmat. The aim is to reshape the matrix into a row vector, repeat it the desired number of times, then reshape it again to regain the two-column matrix.
B = reshape(repmat(reshape(A, 1, []), 2, 1), [], 2);
Note that the 2 within the repmat function controls how many times each row is duplicated. For n times, use reshape(repmat(reshape(A, 1, []), n, 1), [], 2).
Speed
A quick benchmark can be written:
% Setup, using a large A
A = rand(1e5, 2);
f = #() kron(A, [1;1]);
g = #() reshape(repmat(reshape(A, 1, []), 2, 1), [], 2);
% timing
timeit(f);
timeit(g);
Output:
kron option: 0.0016622 secs
repmat/reshape option: 0.0012831 secs
Extended benchmark over different sizes:
Summary:
the reshape option is quicker (~25%) for just duplicating the rows once each, so you should go for this option if you want to end up with 2 of each row for a large matrix.
the reshape option appears to have complexity O(n) for the number of row repetitions. kron has some initial overhead, but is much quicker when you want many repetitions and hardly slows down because of them! Go for the kron method if you are doing more than a few repetitions.

How can I find each max element of three matrices as new matrix?

Maybe the question is a little bit confused, I'll make an example below.
Let's say I have a 3 matrices a, b, c with same size.
a = [2, 5; 6, 9];
b = [3, 3; 8, 1];
c = [5, 5; 2, 7];
How can I get the new matrix max with each max element in all three matrices?
max = [5, 5; 8, 9]
I know I could create logical matrix like a>b and then do the math, calc it out, is there any other more efficient way to do it?
You can concatenate the matrices into one 2x2x3 matrix using
d=cat(3,a,b,c)
and then use max-function to get your desired output:
maxValues=max(d,[],3)
The 3rd input to max defines along which dimension of the first input you want to find the maximum value.

Matlab find the maximum and minimum value for each point of series of arrays (with negative values)

lets say that we have the next series of arrays:
A = [1, 2, -2, -24];
B = [1, 4, -7, -2];
C = [3, 1, -7, -14];
D = [11, 4, -7, -1];
E = [1, 2, -3, -4];
F = [5, 14, -17, -12];
I would like to create two arrays,
the first will be the maximum of each column for all arrays,
i.e.
Maxi = [11,14,-2 -1];
the second will be the minimum of each column for all arrays
i.e.
Mini= [1,1,-17 -24];
I am trying all day, using loops, with max, and abs but I cant make it work
in my problem have a matrix (100,200), so with the above example i am trying to easily approach the problem. The ultimate goal is to get a kinda fitting of the 100 y_lines of 200 x_points. The idea is to calculate two lines (i.e. max,min), that will be the "visual" boarders of all lines (maximum and minimum values for each x). The next step will be to calculate an array of the average of these two arrays, so in the end will be a line between all lines.
any help is more than welcome!
How about this?
Suppose you stack all the row vectors , namely A,B...,F as
arr=[A;B;C;D;E;F];% stack the vectors
And then use the max(), min() and mean() functions provided by Matlab. That is,
Maxi = max(arr); % Maxi is a row vector carrying the max of each column of arr
Mini = min(arr);
Meani = mean(arr);
You just have to stack them as shown above. But if you have 100s of row vectors, use a loop to stack them into array arr as shown above.

How would i vectorize that 'for' loop in Matlab?

I have this toy example:
l = [1, 2, 3, 4, 5];
a = zeros(3, 1);
for p = 1:3
a(p) = sum(l(p:p+2));
end;
This example calculates sum of each 3 elements in 'l', which are close to each other and writes it in 'a'. Now, is it possible to rewrite the same code using vectorization and matrix operations, without 'for' loop? I have tried something similar to:
p = 1:3;
a(p) = sum(l(p:p+2));
The result was [6, 6, 6], it have calculated sum of the first three elements and wrote it in 'a' on each position. This is only a toy example, but i need to do something similar on 3d 256x256x128 array with 20x20x20 cube elements sum, using 'for' loops was VERY slow there. So, is there a fast way to solve this problem?
Edit :
Divakar have suggested me to use my actual case here. Well, here is the most important part of it, which i am trying to rewrite:
Edit2 :
Thank you for answer. Understood the idea, removed code above, rewriting it now.
You're describing a basic convolution:
a = conv(l,ones(1,3),'valid'); % a should have 3 elements
For 3d, use convn in a similar fashion with ones(20,20,20)