필터 지우기
필터 지우기

Replace a value with matrix n x m

조회 수: 3 (최근 30일)
Muammar
Muammar 2011년 12월 5일
Hii.... I want to ask how to replace a value with a matrix n x m. Ex: a = (perms[2])', if a(1)=1, replace a(1)with [1 1;1 1], if a(1)=2, replace it with [2 2;2 2].
Thank you
Muammar

채택된 답변

Walter Roberson
Walter Roberson 2011년 12월 5일
You cannot replace a scalar element of a numeric matrix with a non-scalar.
What you could do is
a = num2cell((perms[2])');
Then
a{1} = a{1} * ones(2,2);
Notice the curly-bracket indexing rather than round-bracket indexing.
  댓글 수: 7
Walter Roberson
Walter Roberson 2011년 12월 5일
Your comment about permutations would seem to indicate that you want to get out the matrix
[[1 1;1 1],[2 2;2 2]; [2 2;2 2], [1 1; 1 1]]
which would be
[1 1 2 2; 1 1 2 2; 2 2 1 1; 2 2 1 1]
which would be a 4 x 4 matrix, not a 1 x 2 matrix.
My *guess* is that what you really are looking for is ndgrid:
[P,Q] = ndgrid(numel(data), numel(data));
P = P(:);
Q = Q(:);
Then permutation #K would be
[data(P(K)), data(Q(K))]
so the n x 2 matrix formed by [P,Q] would be pairwise indices in to the data:
PQ = [P,Q];
the K'th row of which would be used to fetch the K'th permutation:
data(PQ(K,:))
Muammar
Muammar 2011년 12월 5일
I will try this one, actually what i need is similar to your first answer,"a{1} = a{1} * ones(2,2);", but i cant make it a numerical values. But, thank you..

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 12월 5일
Maybe:
data1 = ones(2);
data2 = magic(2);
D = [data1;data2];
D2 = reshape(flipdim(reshape(D',size(data1,1),size(data1,2),[]),3),size(D))
?
  댓글 수: 9
Muammar
Muammar 2011년 12월 5일
All.. I am sorry if its not clear
Muammar
Muammar 2011년 12월 5일
it should be 2 x 2 matrix

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2011년 12월 5일
I agree with the others - unclear. Perhaps you mean this:
m = randi(9, [1 4]) % Sample row vector with random integers
m2 = imresize(m, 2, 'nearest') % Do the replication.
Results:
m =
7 8 2 5
m2 =
7 7 8 8 2 2 5 5
7 7 8 8 2 2 5 5
So maybe you can just simply use imresize to get what you want.
  댓글 수: 1
Muammar
Muammar 2011년 12월 5일
I am so sorry if it is not clear

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by