How can i manipulate a matrix with this characteristics?

1 回表示 (過去 30 日間)
Vitor Ribeiro
Vitor Ribeiro 2014 年 4 月 9 日
コメント済み: Vitor Ribeiro 2014 年 4 月 10 日
Hi, i have a matrix wtih repeated elements like:
[1 4 1 4 2 3 3 5 5 6 6 5]
i want break that matrix. And then in each other matrix, running in a cycle, have only the index referent to each number (1, 2, 3, ...), something like:
1 2 3 4 5 6
[1 4] [2 3] [2 3 [1 4] [3 5 [5 6
3 5] 5 6 6 5]
6 5]
I tried creating a 3 dimension matrix (i,j,k) and each 3rd dimension having above matrix. That hypothesis gives me the repeated solutions inside. I tried to get them out but I might not be able to.
I hope someone can help me out.
PS: Some question that may have, just do it pls.
Regards, Vitor Ribeiro.

採用された回答

Joseph Cheng
Joseph Cheng 2014 年 4 月 9 日
編集済み: Joseph Cheng 2014 年 4 月 9 日
You can use cells for this. I do not see the pattern you have for [1 4 1 4 2 3 3 5 5 6 6 5] to the broken up matrix. but to set the broken up matrix with index references, cells should work.
MATRIX{1} = [1 4];
MATRIX{2} = [2 3];
MATRIX{3} = [2 3;3 5];
etc.
Which should be simple enough to accomplish in a for loop with whatever initial matrix breakup pattern is.
  1 件のコメント
Vitor Ribeiro
Vitor Ribeiro 2014 年 4 月 10 日
I have implemented this code, following your ideia.
z=1;
for a=1:size(bus,1)
z=1;
for i=1:size(branch_,1)
for j=1:2
if branch_(i,j)==bus(a)
brx{a,z}=branch_(i,1:2);
z=z+1;
end
end
end
end
Now i need to ensure that in the matrix brx i don't have repeated contents in each row.
[1,4] [1,4] [] [] [] []
[8,2] [8,2] [] [] [] []
[3,6] [3,6] [] [] [] []
[1,4] [1,4] [4,5] [9,4] [4,5] [9,4]
[4,5] [4,5] [5,6] [5,6] [] []
[3,6] [5,6] [5,6] [3,6] [6,7] [6,7]
[6,7] [6,7] [7,8] [7,8] [] []
[8,2] [7,8] [7,8] [8,2] [8,9] [8,9]
[9,4] [8,9] [8,9] [9,4] [] []
Considering that the real aspect of inside matrix brx is represented above. I tried to do a comparison with a conditional structure if. Just like:
if brx{i,j}==brx{i,j+1}
brx{i,j+1}={};
end
But in fact, a comparison between this 2 elements results in a [1 1] matrix, cause it compares each one of the 2 element of inside cells of the matrix brx.
If you know a way to eliminate repeated cells of brx matrix I appreciate you answer me.
I tried below code, and obviously did not result. I have no experience working with cells. Hope someone can help me out.
for i=1:size(brx,1)
for j=1:size(brx,2)-1
for k=1:size(brx,1)
if brx{i,j}==brx{i,k}
brx{i,j+1}={};
end
end
end
end
Best reagards, Vitor Ribeiro.

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

その他の回答 (1 件)

Vitor Ribeiro
Vitor Ribeiro 2014 年 4 月 10 日
I have implemented this code, following your ideia.
z=1;
for a=1:size(bus,1)
z=1;
for i=1:size(branch_,1)
for j=1:2
if branch_(i,j)==bus(a)
brx{a,z}=branch_(i,1:2);
z=z+1;
end
end
end
end
Now i need to ensure that in the matrix brx i don't have repeated contents in each row.
[1,4] [1,4] [] [] [] []
[8,2] [8,2] [] [] [] []
[3,6] [3,6] [] [] [] []
[1,4] [1,4] [4,5] [9,4] [4,5] [9,4]
[4,5] [4,5] [5,6] [5,6] [] []
[3,6] [5,6] [5,6] [3,6] [6,7] [6,7]
[6,7] [6,7] [7,8] [7,8] [] []
[8,2] [7,8] [7,8] [8,2] [8,9] [8,9]
[9,4] [8,9] [8,9] [9,4] [] []
Considering that the real aspect of inside matrix brx is represented above. I tried to do a comparison with a conditional structure if. Just like:
if brx{i,j}==brx{i,j+1}
brx{i,j+1}={};
end
But in fact, a comparison between this 2 elements results in a [1 1] matrix, cause it compares each one of the 2 element of inside cells of the matrix brx.
If you know a way to eliminate repeated cells of brx matrix I appreciate you answer me.
I tried below code, and obviously did not result. I have no experience working with cells. Hope someone can help me out.
for i=1:size(brx,1)
for j=1:size(brx,2)-1
for k=1:size(brx,1)
if brx{i,j}==brx{i,k}
brx{i,j+1}={};
end
end
end
end
Best reagards, Vitor Ribeiro.

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by