Replicating a matrix in matlab

2 ビュー (過去 30 日間)
Ede gerlderlands
Ede gerlderlands 2013 年 6 月 17 日
I have a matrix v=(1:2400,1). I want to replicate each element of v by the corresponding element in p=(1:2400). Here is the function I used to do the same.
for t=1:2400;
A(t,:)= repmat(v(t,1), 1, p(t));
end
But there is an error message which says ' Subscripted assignment dimension mismatch ' keeps coming. What can I do about this?
  1 件のコメント
Muthu Annamalai
Muthu Annamalai 2013 年 6 月 17 日
Have you thought about using 'kron' and 'repmat' to rewrite your problem as a series of large matrix products? It requires some rearrangement on your part but it something that'll payoff nicely.

サインインしてコメントする。

採用された回答

Sean de Wolski
Sean de Wolski 2013 年 6 月 17 日
dbstop if error
Then run your code. Look at each side of the parenthesis and I'm sure you'll see that:
size(A(t,:))
Is different than:
size(repmat(v(t,1),1,p(t)))
This makes sense too, p(t) cannot change size or you will see this error.
Perhaps you mean
A(t,1:p(t))?
i.e. fill the first p(t) elements with whatever repmat spits out.
  1 件のコメント
Ede gerlderlands
Ede gerlderlands 2013 年 6 月 17 日
Thank you . I come to know my mistake

サインインしてコメントする。

その他の回答 (1 件)

Evan
Evan 2013 年 6 月 17 日
編集済み: Evan 2013 年 6 月 17 日
Because the amount you're replicating each element of V by varies according to p, the resulting rows in A are not of the same size. As you create a regular matrix with variable row sizes, you're getting that error.
One way to get around this would be using a cell array instead of a normal array. Each cell would contain a row of your matrix A.
A = cell(2400,1);
for t=1:2400;
A{t,:} = repmat(v(t,1), 1, p(t));
end
Of course, there are reasons for and against doing this. Another option would be to pad the rows of your matrix A with zeros so that they're all the same size. But this might not be a good idea depending on what you're data in v is and what you're going to be doing with it.
Note: it looks like the code Sean posted above pads the matrix with zeros like I mentioned.
  5 件のコメント
Ede gerlderlands
Ede gerlderlands 2013 年 6 月 17 日
ok Thanks...what if I want to plot this in 2d is it not possible to change to matrix form?
Evan
Evan 2013 年 6 月 17 日
You can change to matrix form but, for the same reasons as your error above, not all at once.
However, you can access the different cells of your cell array one at a time, so plotting the data using a for loop might work.
When you create your cell array, you'll get an array that's something like this for A:
[1 x p(1) double]
[1 x p(2) double]
[1 x p(3) double]
...
And so on. To access the contents of individual cells, call A{i}. So, if you needed to plot, you could do something like this:
for i = 1:length(A)
plot(x,A{i})
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by