Matlab:如何在两个方向上用A的其他值替换矩阵A的某些元素? [英] Matlab: How to replace certain elements of a matrix A by other values of A in both directions?
本文介绍了Matlab:如何在两个方向上用A的其他值替换矩阵A的某些元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
用[47 78 41 1 99 98]替换[5 7 9 18 55 4]和用[5 7 9 18 55 4]替换[47 78 41 1 99 98]的数字
解决方案
使用 ismember
:
n1 = [1 2 3]; %// first set of numbers
n2 = [4 5 6]; %// second set of numbers
[v1, i1] = ismember(A,n1);
[v2, i2] = ismember(A,n2);
A(v1) = n2(i1(v1));
A(v2) = n1(i2(v2));
示例:
>> A = randi(8,4,5)
A =
2 2 8 4 6
2 5 3 8 2
5 4 3 2 5
4 3 2 3 4
被转化为
A =
5 5 8 1 3
5 2 6 8 5
2 1 6 5 2
1 6 5 6 1
for a matrix A (10x100000) containing numbers between 1 and 100, how to interchange some elements of A by other values of A in both directions?
example:
replace numbers [5 7 9 18 55 4] by [47 78 41 1 99 98] and [47 78 41 1 99 98] by [5 7 9 18 55 4]
解决方案
Use the two outputs of ismember
:
n1 = [1 2 3]; %// first set of numbers
n2 = [4 5 6]; %// second set of numbers
[v1, i1] = ismember(A,n1);
[v2, i2] = ismember(A,n2);
A(v1) = n2(i1(v1));
A(v2) = n1(i2(v2));
Example:
>> A = randi(8,4,5)
A =
2 2 8 4 6
2 5 3 8 2
5 4 3 2 5
4 3 2 3 4
is transformed into
A =
5 5 8 1 3
5 2 6 8 5
2 1 6 5 2
1 6 5 6 1
这篇关于Matlab:如何在两个方向上用A的其他值替换矩阵A的某些元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文