Hello everybody !
I have a vector A and a matrix B
A= [10 20 30]
B= 2 5 10
4 10 20
6 15 30
so if I want to calculate the following matrix : here I want to substract the first elment of the vector to the first column by and then the second element to the second columns and the third element to the third column..by this way :
C= 10-2 20-5 30-10
10-2-4 20-5-10 30-10-20
10-2-4-6 20-5-10-15 30-10-20-30
So as shown, I want to substract the first element of the vector to the first column elements , substract the first element, keep result then substract to the second element, keep result, substract to the third element .. Thank you

 採用された回答

Jan
Jan 2017 年 3 月 13 日
編集済み: Jan 2017 年 3 月 13 日

1 投票

I assume that the FOR loop is not essential.
Result = A - cumsum(B, 1); % Matlab 2016b autoexpanding
For older Matlab versions:
Result = bsxfun(@minus, A, cumsum(B, 1));

3 件のコメント

heir ancestors
heir ancestors 2017 年 3 月 13 日
Hello Jan thank you very much ;
The code is working great ! Just in my case it's
Result=bsxfun(@minus,A',cumsum(B,1));
Jan
Jan 2017 年 3 月 13 日
You are welcome. I love Matlab for this compact coding. This would be a small battle in C!   :-(
heir ancestors
heir ancestors 2017 年 3 月 13 日
oh yes :D , thank you again :)

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2017 年 3 月 13 日

コメント済み:

2017 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by