フィルターのクリア

I want find the sum of all the elements of the matrix, how to do it?(with sum() function)

119 ビュー (過去 30 日間)
hp
hp 2017 年 12 月 27 日
回答済み: Steven Lord 2020 年 3 月 4 日
I need to find the sum of half of the rows and all the columns... if i write only sum(a) it will give as below .. actually i need a single value of sum of pixels...
a=imresize((imread('G:\matlab2013\Crack\bin\gm.jpg')),[8 4]);
>> z=sum(a)
z(:,:,1) =
1898 1531 1367 1713
z(:,:,2) =
1734 1093 1145 1724
z(:,:,3) =
1793 1171 1199 1830
if i write as below i am getting single value but,
>> z=sum(a(:))
z =
18198
i need only sum of half of the rows and all the columns...
>> z=sum(a(1:mid-0f-row-,1:allcolums)
if i write above code it gives 1row multiple column values, that i dont need.. i have to get single value after summing those pixels as shown in the image by brown area... first half rows and all the columns covering those rows.. and should get single value for summing that area pixel values... thank you..

回答 (3 件)

Hiranmay Das
Hiranmay Das 2020 年 3 月 4 日
sum(sum(X))
Above code would do the job.

Steven Lord
Steven Lord 2020 年 3 月 4 日
This wasn't an option at the time the question was originally asked, but if you're using release R2018b or later you can use the capability introduced in that release to specify either a vector of dimensions or the option 'all' to sum over multiple (or all) dimensions of an array.
A = magic(4);
sum(A, 'all')

Matt J
Matt J 2017 年 12 月 27 日
編集済み: Matt J 2017 年 12 月 27 日
z=sum( reshape( a( 1:mid-0f-row-,1:allcolums,:) [],1 ) );

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by