필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
melampyge
melampyge 2013년 8월 12일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by