search for part of matrix and change arrangement of matrix

1 回表示 (過去 30 日間)
Raed Alahmadi
Raed Alahmadi 2019 年 1 月 3 日
コメント済み: Raed Alahmadi 2019 年 1 月 4 日
Hello,
I have vector A
A=[1 2 3 4 5 6];
and I have matrix B
B= [9 7 4;3 2 1;8 22 4;5 6 24]
Matrix A is unchangeable while size of B is unchangeable but its arrangement is okay to change. B is considered as initial guess so when it order is change it should change slightly and for a reason. transfaring from a row to a new row in B means three zeros. so if I want to write it in a vector for it will be:
B1=[9 7 4 0 0 0 3 2 1 0 0 0 8 22 4 0 0 0 5 6 24 ];
now my condition:
if three consecutive element of vector A is present in six consecutive column in vector B1 then vector B1 should chnge its order by take the last column that couse the problem in B1 and exchange with last+1 column and if last+1 does not exist then by take the first column that couse the problem in B1 and exchange with first-1 column. However, the elements [0 0 0] should not be exchange and it should be in its place all the time. After finishing all changing in vector B1 and the condition that no three consecutive element of vector A is present in six consecutive column in vector B1 then the code take vector B1 to be a matrix with the same size as it was before B.
In the above example the problem in be exist in two places 1st one [3 2 1] and 2nd one [4 0 0 0 5 6].
I spent a lot of hours trying to code all this conditions but unfortunately my code dose not work. Please help me.
Thanks.
  1 件のコメント
Raed Alahmadi
Raed Alahmadi 2019 年 1 月 4 日
I finished the first part which is convert B to vector
A=[1 2 3 4 5 6];
B= [9 7 4;3 2 1;8 22 4;5 6 24];
c= size(B);
cunt=1;
x=zeros(1,c(1,2)*c(1,1)+3*(c(1,1)-2));
for i=1:c(1,1)
B_row=[B(i,:)];
pcunt=cunt;
cunt=cunt+(c(1,2)-1);
x(1,pcunt:cunt)= B_row;
cunt=cunt+1+3;
end
x
Please help me of how to force the vector B to change its order depend on my condition.
Thanks

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by