How can I create a specific matrix in a for loop?

Hi,
I have a 4x50 matrix which is defined as x. I'm trying to store each row of x inside each row of b with a formula that is stated in the code below. So, each row of b is predetermined with the corresponding row of x. After creating the matrix b, I will generate arrays with the code ndgrid. I keep getting the error "Subscripted assignment dimension mismatch." when it comes to the for loop. How I can get around this problem? Thank you very much in advance.
[M N] = size(x); % M number of rows, N number of column
b = zeros(M,N);
b(1,:) = x(1,:);
for j=1:1:M-1
b(j,:) = [min(x(j,:)):(max(x(j,:))-min(x(j,:)))/20:max(x(j,:))];
end
binscenter(1:M,:) = ndgrid(b(1:M,:));
Edit: Also in the last line with the code ndgrid, how can I generate arrays? I don't think my code is going to work, since I didn't specify what binscenter is. The problem is, number of rows and columns of x can change under different circumstances. So I'm trying to write a more compact code which calculates number of rows of x and than binscenter is determined accordingly.

1 Commento

I suggest you resolve your first problem, and then post your second question separately.

Accedi per commentare.

 Risposta accettata

the cyclist
the cyclist il 12 Ago 2013
Modificato: the cyclist il 12 Ago 2013
The right-hand side of your assignment statement inside the for loop is creating a vector that is length 21, and the right-hand side is length 50. That is the mismatch.
Instead of "20", you need "N-1" there.
You might still need to be careful in how you constructed that statement, because creating vectors like that can lead to unexpected results when using floating point numbers. I suggest you use the linspace() function to do that operation:
linspace(min(x(j,:)),max(x(j,:)),N)
should do what you want.

6 Commenti

linspace did the trick a couple of minutes ago, but strangely now it doesn't work. I changed it to:
b(1,:) = linspace(min(x(1,:)),max(x(1,:)),N);
for j=1:1:M-1
b(j,:) = linspace(min(x(j,:)),max(x(j,:)),N);
end
binscenter = ndgrid(b(1:M,:));
Are there any rows where all the values are equal, such that the min and max are the same? In that case, your right-hand side will be empty, and therefore you will get the dimension mismatch again.
Also, don't you want
for j = 2:1:M
?
I checked again but there aren't any rows with all the values equal.
the cyclist
the cyclist il 12 Ago 2013
Modificato: the cyclist il 12 Ago 2013
I suggest you use debug mode to look at the state of your code when you get the error. Here's a document that describes how to do that: http://www.mathworks.com/help/matlab/debugging-code.html
Also, if you could post a very small example of x that exhibits the problem, then maybe I could take a look at that specific example. Just creating the smallest possible example that exhibits the problem may help you find the problem, too.
Sure, I will try doing debugging. x is a 3x1001 matrix at the moment, I will now try with a smaller matrix than to see what happens.

Accedi per commentare.

Più risposte (1)

what's the reason behind the "20" in the 5th line of your code?
[M N] = size(x); % M number of rows, N number of column
b = zeros(M,N);
b(1,:) = x(1,:);
for j=1:M-1
b(j,:) = min(x(j,:)):(max(x(j,:))-min(x(j,:)))/20:max(x(j,:));
end
if your x matrix is 20x20, this part of the code will work as follows:
x = rand(20); % example matrix
[M N] = size(x); % M number of rows, N number of column
b = zeros(M,N);
b(1,:) = x(1,:);
for j=1:M-1
b(j,:) = min(x(j,:)):(max(x(j,:))-min(x(j,:)))/19:max(x(j,:));
end
About your last line, is this what you want?
binscenter = ndgrid(b(1:M,:));

1 Commento

It doesn't have to be 20 per se, I can change it to 50 for example. I'm calculating drift in a stochastic differential equation, so I need to generate as many points as I can. For the last line, that is what I want, thank you!

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by