Multiplying even and odd columns separately?

I have a matrix called 'displacements'
It has 50 columns
I want to multiply all the even columns by 5 and all the odd columns by 8
Anyone know a quick way to do this?

回答 (2 件)

Stephen23
Stephen23 2015 年 3 月 6 日
編集済み: Stephen23 2015 年 5 月 22 日

2 投票

You could do this directly using matrix indexing :
>> A = rand(4,50);
>> A(:,1:2:end) = A(:,1:2:end) * 8;
>> A(:,2:2:end) = A(:,2:2:end) * 5;
Or using bsxfun and repmat:
>> B = bsxfun(@times, A, repmat([8,5],1,25));
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2015 年 3 月 6 日

1 投票

Here is one way:
A =rand(100, 50);
idx = mod(1:size(A,2), 2) ==1; % logical indexing of the odd numbers
theodd = A(:, idx)*8;
theeven = A(:, ~idx)*5;

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2015 年 3 月 6 日

編集済み:

2015 年 5 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by