populate matrix columns based on a vector of index vector

7 ビュー (過去 30 日間)
MatG
MatG 2020 年 6 月 12 日
編集済み: Stephen23 2020 年 6 月 12 日
I generate an 8x8 A matrix below. I also have an 8x1 vector B containing random integer numbers from 1 to 8. And, I have an 8x1 C vector containing numbers obtained below. How can I, for all rows of A, populate those columns of A starting at B to the end, with values of corresponding rows in C without going through a loop (i.e. vectorized).
A=rand(8);
B = randi([1 8], 8, 1);
C=-linspace(8,30,8)';
  2 件のコメント
KSSV
KSSV 2020 年 6 月 12 日
Confused.....what out put you are expecting?
MatG
MatG 2020 年 6 月 12 日
Imagine
A = [
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5]
And
B = [1 3 4 2 5]
C = [-10 -9 -5 -2 -1];
Then, I want A(1,B(1):end) = C(1), A(2,B(2):end) = C(2), ....
So I get:
A = [-10 -10 -10 -10 -10
1 2 -9 -9 -9
1 2 3 -5 -5
1 -2 -2 -2 -2
1 2 3 4 -1]

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

採用された回答

Stephen23
Stephen23 2020 年 6 月 12 日
編集済み: Stephen23 2020 年 6 月 12 日
>> X = (1:5)>=B(:); % requires >=R2016b, for earlier versions replace >= with BSXFUN
>> [R,~] = find(X);
>> A(X) = C(R)
A =
-10 -10 -10 -10 -10
1 2 -9 -9 -9
1 2 3 -5 -5
1 -2 -2 -2 -2
1 2 3 4 -1

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by