How can I create a specific matrix in a for loop?
Mostra commenti meno recenti
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
the cyclist
il 12 Ago 2013
I suggest you resolve your first problem, and then post your second question separately.
Risposta accettata
Più risposte (1)
David Sanchez
il 12 Ago 2013
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
melampyge
il 12 Ago 2013
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!