Substitute numbers in array

How are you everyone!
I have the array X=[1 2 3 4 5 6 7 8] I want to flip this array and change the numbers as next
1 to 5 and 5 to 1
2 to 6 and 6 to 2
3 to 7 and 7 to 3
4 to 8 and 8 to 4
So the result which I want after flipping and substitution is
XX=[4 3 2 1 8 7 6 5]

回答 (4 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 22 日
編集済み: Azzi Abdelmalek 2016 年 3 月 22 日

0 投票

X=[1 2 3 4 5 6 7 8]
XX=[fliplr(X(1:4)) fliplr(X(5:end))]

7 件のコメント

majed majed
majed majed 2016 年 3 月 22 日
編集済み: Azzi Abdelmalek 2016 年 3 月 22 日
Thanks for you answer Sometimes maybe the array doesn't has the last form , it cold be like this
X=[1 3 6 5 3 3 2 8 7 6 5]
Or
X=[5 6 4 3 7 8 3 2 1 7 8]
How we can solve it? Thank you again
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 22 日
Ok, for this example X=[1 2 3 4 5 6 7 8 9 10 11], what are you expecting as result?
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 22 日
編集済み: Azzi Abdelmalek 2016 年 3 月 22 日
Maybe you want this:
X=[1 2 3 4 5 6 7 8 9 10 11]
n=fix(numel(X)/2)
XX=[fliplr(X(1:n)) fliplr(X(n+1:end))]
majed majed
majed majed 2016 年 3 月 22 日
The result I want is as I mentioned befor, just substitute the numbers as I've mentioned and flip the array
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 22 日
This case is different, there are 11 element (11 is odd)
majed majed
majed majed 2016 年 3 月 22 日
The numbers or values is just from 1 to 8
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 22 日
Now if you want to flip just 8 element:
X=[1 2 3 4 5 6 7 8 9 10 11]
XX=[fliplr(X(1:4)) fliplr(X(5:8)) X(8+1:end)]

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

Stephen23
Stephen23 2016 年 3 月 22 日
編集済み: Stephen23 2016 年 3 月 22 日

0 投票

Try this function. The matrix M defines any arbitrary values to swap. Note that these values are not used as indices so this is a general solution to the problem.
>> M = [1:4;5:8].'; % each row specifies one pair of values to swap
M =
1 5
2 6
3 7
4 8
>> fun = @(X)fliplr(reshape(M(:,[2,1]),1,[])*bsxfun(@eq,X,M(:)));
and the examples you gave are:
>> fun([1,2,3,4,5,6,7,8])
ans =
4 3 2 1 8 7 6 5
>> fun([1,3,6,5,3,3,2,8,7,6,5])
ans =
1 2 3 4 6 7 7 1 2 7 5
>> fun([5,6,4,3,7,8,3,2,1,7,8])
ans =
4 3 5 6 7 4 3 7 8 2 1
Jan
Jan 2016 年 3 月 22 日
編集済み: Jan 2016 年 3 月 22 日

0 投票

A job for a lookup table:
X = [1, 2, 3, 4, 5, 6, 7, 8]
LUT = [5, 6, 7, 8, 1, 2, 3, 4]
Result = LUT(fliplr(X))

2 件のコメント

Stephen23
Stephen23 2016 年 3 月 22 日
編集済み: Stephen23 2016 年 3 月 22 日
Nice use of indexing :)
Note that it is not a general solution suitable for all data: if
X = [1,1e9]
what will LUT have to be?
Steven Lord
Steven Lord 2016 年 3 月 22 日
In that case I would make the LUT a sparse column vector.
X = [1, 1e9];
LUT = sparse(X, 1, [2, 73]);
Y = full(flip(LUT(X)))

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

Suraj Sudheer Menon
Suraj Sudheer Menon 2020 年 6 月 22 日

0 投票

The following could be an approach:-
sub=[5 6 7 8 1 2 3 4];
XX=flip(X);
for i=1:numel(X)
XX(i)=sub(XX(i));
end
%XX contains neccesary values.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2016 年 3 月 22 日

回答済み:

2020 年 6 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by