Change element in a vector

Hi, I want change the elements: 2 with 6 and 6 with 2 in the first column of a matrix A
A=[1,2,4;2,3,5;6,4,6;2,5,6]
with a structure like this
A(A(:,1)==2)=6;
How can I do?
Thanks!

回答 (2 件)

KSSV
KSSV 2017 年 5 月 9 日

1 投票

A=[1,2,4;2,3,5;6,4,6;2,5,6] ;
B = A ;
A(B(:,1)==2,1) = 6 ;
A(B(:,1)==6,1) = 2 ;
Stephen23
Stephen23 2017 年 5 月 9 日
編集済み: Stephen23 2017 年 5 月 9 日

0 投票

Without copying the array:
>> A = [1,2,4;2,3,5;6,4,6;2,5,6]
A =
1 2 4
2 3 5
6 4 6
2 5 6
>> idx = A==6;
>> A(A==2) = 6;
>> A(idx) = 2
A =
1 6 4
6 3 5
2 4 2
6 5 2

カテゴリ

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

タグ

質問済み:

2017 年 5 月 9 日

編集済み:

2017 年 5 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by