フィルターのクリア

problem in Matrix Indexing

1 回表示 (過去 30 日間)
sita
sita 2012 年 11 月 22 日
Hi, below code i am trying to read matrix elements from an array of elements. i should get 5x3 matrix like
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
but i am gettting like below
1 2 3
1 2 3
1 2 3
1 2 3
3 3 3
i understand there is some problem with indexing.
Please help me in getting this.
i got some answer saying that to remove f(i,:)=k(x) if i do that f is 1 2 3 it is only 1X3 matrix i need it to be 5X3.
i dont want to use repmat because i have to use this in other context where i can not use.
Thanks,
Sita
n=5; v=3; k=[1 2 3];
for i=1:n
x=0;
for j=1:1:v
x=x+1;
f(:,j)= k(x);
end
f(i,:)=k(x)
end
  2 件のコメント
José-Luis
José-Luis 2012 年 11 月 22 日
Remove
f(i,:) = k(x);
The result of your loop will be a 5x3 matrix. It will be a 1x3 matrix only on the first iteration. Consider preallocating for speed.
Walter Roberson
Walter Roberson 2012 年 11 月 22 日

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

採用された回答

vipul utsav
vipul utsav 2012 年 11 月 22 日
編集済み: Walter Roberson 2012 年 11 月 22 日
n=5; v=3; k=[1 2 3];
for i=1:n
x=0;
for j=1:v
x=x+1;
f(i,j)= k(x);
end
end

その他の回答 (1 件)

Arthur
Arthur 2012 年 11 月 22 日
Well, if you insist not to use repmat (why??), I'd do it like this:
f = zeros(n,v);
for i = 1:v
f(:,i) = k(i);
end
  1 件のコメント
Jan
Jan 2013 年 2 月 1 日
Or:
k = [1,2,3];
f = k(ones(1,v), :);

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by