How can I create a specific matrix in a for loop?
2 ビュー (過去 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
2013 年 8 月 12 日
I suggest you resolve your first problem, and then post your second question separately.
採用された回答
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
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.
その他の回答 (1 件)
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,:));
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!