how to change a variable?

2 ビュー (過去 30 日間)
Rengin
Rengin 2015 年 4 月 27 日
回答済み: Thorsten 2015 年 4 月 27 日
clear all
clc
nrow=5;
ncolumn=3;
A=[12 28 35;42 14 30;16 25 13;21 34 12;32 18 26];
B=[14 26 18;17 41 32;19 23 34;36 38 17;25 18 16];
C=sum(A,2);
D=sum(B,2);
SumLast=zeros(nrow,1);
for i=1:nrow
for j=1:ncolumn
% if jth (2.) column in ith (3.) row in A matrix bigger than
% jth (2.) column in ith (3.) row in B matrix, I want to put ith
% (3.) row of D matrix in ith (3.) row of LastSum
if (A(i,j)>B(i,j))
SumLast(i)=D(i);
% Otherwise, I want to meet the condition below:
else
SumLast(i)=C(i);
end
end
end
% I want to have LastSum=[58 90 76 67 59] but instead
% I get LastSum=[58 86 54 67 59]
% How can i correct that script?
% Many Thanks!
  2 件のコメント
Michael Haderlein
Michael Haderlein 2015 年 4 月 27 日
Sorry, I don't understand how you select the value you want to put to SumLast. In your loop, you overwrite the value two times, I guess this is not intended. Best would be to just place a breakpoint at your first for and then use single step mode (F10 to forward one line) and see what happens.
Rengin
Rengin 2015 年 4 月 27 日
Basically if an element in A matrix is bigger than the corresponding element in B matrix, I make my selection from D matrix. Otherwise, from C matrix. If one element in a row in A matrix is bigger than the corresponding element in B matrix, without looking at other elements in the row, I go to the following row. When you execute the script and look at my example, you'll understand what I mean

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

回答 (1 件)

Thorsten
Thorsten 2015 年 4 月 27 日
SumLast = D;
ind = ~any(A>B, 2);
SumLast(ind) = C(ind);

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by