Subtracting elements in an array
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
hi there,
I have an array lets say
A=[10 9 10; 1 2 3; 4 5 6]
How can create an array like this?
B=[10-1 9-2 10-3;10-1-4 9-2-5 10-3-6]
thanks in advance!
Nikolas
採用された回答
Star Strider
2017 年 1 月 21 日
編集済み: Star Strider
2017 年 1 月 21 日
If I understand correctly what you are asking, this will work:
A=[10 9 10; 1 2 3; 4 5 6]
B = cumsum(-A(2:end,:))+A(1,:)
A =
10 9 10
1 2 3
4 5 6
B =
9 7 7
5 2 1
9 件のコメント
Walter Roberson
2017 年 1 月 22 日
Alternately
A(1,:) - cumsum(A(2:end,:))
My first mental version was indeed in terms of negating and then cumsum, but then I realized that wasn't necessary.
Nikolas Spiliopoulos
2017 年 1 月 22 日
Yeah that's exactly what i mean thanks a lot!!
Star Strider
2017 年 1 月 22 日
Our pleasure!
Nikolas Spiliopoulos
2017 年 1 月 22 日
hi again, I tried it but I get an error "matrix dimensions must agree"! any ideas how to fix it? thanks
Star Strider
2017 年 1 月 22 日
Please post your matrix and code. That error should not occur with the sort of matrix you originally posted.
Nikolas Spiliopoulos
2017 年 1 月 22 日
編集済み: Nikolas Spiliopoulos
2017 年 1 月 22 日
>> A=[10 9 10; 1 2 3; 4 5 6] B = cumsum(-A(2:end,:))+A(1,:)
A =
10 9 10
1 2 3
4 5 6
Error using "+ " Matrix dimensions must agree.
Star Strider
2017 年 1 月 22 日
The most recent versions of MATLAB do this minimal expansion automatically. Somewhat less recent versions require the bsxfun function to do the same operation.
This should work for you:
B = bsxfun(@minus, A(1,:), cumsum(A(2:end,:)));
(I tested it, and it gives the same result as our eariler code versions.)
Nikolas Spiliopoulos
2017 年 1 月 22 日
ok got it, it works! thanks again mate..!!!
Star Strider
2017 年 1 月 22 日
Our pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
