フィルターのクリア

logical operations on particular matrix elements

1 回表示 (過去 30 日間)
Christopher
Christopher 2015 年 8 月 9 日
編集済み: Azzi Abdelmalek 2015 年 8 月 9 日
I have the following code:
numels = 7;
numpts = 3;
C = sparse(numpts,numels)
A = randi(numels,[numpts,1]);
B = rand(numpts,1);
I want to treat the matrix A as an index for the columns of matrix C and move the values of B to their respective columns.
So if we have:
A = [3;3;6];
B = [0.383;0.892;0.192];
Then we should be able to get:
full(C) =
0 0 0.383 0 0 0 0
0 0 0.892 0 0 0 0
0 0 0 0 0 0.192 0
I thought that
C(:,A)=B;
might work, but C(:,A) attempts refers to a matrix and not a set of values.
BTW, I want a logical operation, I don't want to use accumarray or something. It is important that it is fast.
Any help is appreciated.
  1 件のコメント
Christopher
Christopher 2015 年 8 月 9 日
I've found that I can just find the indices in the sparse matrix and thus use the following:
newA = (A-1).*numpts+(1:numpts)';
C(newA)=B;
However, I've found that using larger matrices of C (required for my implementations), execution of C(newA)=B; is EXTREMELY SLOW.
How can I make this operation faster?

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

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 8 月 9 日
編集済み: Azzi Abdelmalek 2015 年 8 月 9 日
numels = 7;
numpts = 3;
C = sparse(numpts,numels)
A = [3;3;6];
B = [0.383;0.892;0.192]
idx=sub2ind(size(C),1:numel(A),A')
C(idx)=B
full(C)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by