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

4 次查看(过去 30 天)
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 个评论
the cyclist
the cyclist 2013-8-12
I suggest you resolve your first problem, and then post your second question separately.

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2013-8-12
编辑:the cyclist 2013-8-12
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 个评论
the cyclist
the cyclist 2013-8-12
编辑:the cyclist 2013-8-12
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.
melampyge
melampyge 2013-8-12
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.

请先登录,再进行评论。

更多回答(1 个)

David Sanchez
David Sanchez 2013-8-12
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 个评论
melampyge
melampyge 2013-8-12
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by