How to interchange/swap variable in matrix and store it to different variable.

3 ビュー (過去 30 日間)
Triveni
Triveni 2016 年 1 月 26 日
編集済み: Triveni 2016 年 1 月 26 日
p = [90 90 -45 0 0 45 45 0 -45];
p1 = p; %store temporary copy of p
idx = [true diff(p)~=0];
q = p(idx);
idx = fliplr([true, diff(fliplr(p))~=0]);
position_p = find(idx); pos = position_p;
I want to interchange positions of p according to position_p from right side. In every iteration only p(9) should be interchange.
p(9) interchange with p(8) //A/c to position_p right to left
Layer(:,:,1) = [90 90 -45 0 0 45 45 -45 0];
p(9) interchange with p(7)
Layer(:,:,2) = [90 90 -45 0 0 45 0 -45 45];
and so on...
position_p = [2 3 5 7 8 9] menas 1st interchange p(9) with p(8) then store layer and then interchange p(7) and so on....

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 1 月 26 日
編集済み: Andrei Bobrov 2016 年 1 月 26 日
ii = find(idx);
n = numel(ii);
out = zeros(n,numel(p));
out(1,:) = p;
i1 = fliplr(ii);
for jj = 2:n
out(jj,:) = out(jj-1,:);
out(jj,[i1(jj),end]) = out(jj,[end,i1(jj)]);
end
  2 件のコメント
Triveni
Triveni 2016 年 1 月 26 日
Lot of thanks Sir...
Triveni
Triveni 2016 年 1 月 26 日
Can you reshape out to Layer(:,:,k) except 1st layer

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeExternal Language Interfaces についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by