Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Martix help within a for loop

2 ビュー (過去 30 日間)
Christopher
Christopher 2013 年 5 月 5 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello,
I have a 2 by 1000 matrix. I want to wrtie and if statement saying if A(2,:)=1 then to spit out the corresponding A(1,:) value. Once i have all my A(1,:) values that correspond to A(2,:) = 1, i will add them all.

回答 (1 件)

Image Analyst
Image Analyst 2013 年 5 月 5 日
Try this:
% Create random sample data.
m = int32(randi(9, [2, 1000]))
% Find out which columns have a 1 in row 2
columnsThatAre1 = m(2, :) == 1
theFirstRowvalues = m(1, columnsThatAre1)
% Sum them up
theSum = sum(theFirstRowvalues)
It should be self-explanatory and easy to follow with comments and descriptive variable names. Of course you could do it all in one compact line, but I thought you'd find this easier to follow.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by