Replace a value with matrix n x m

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 日

0 投票

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 件のコメント

Muammar
Muammar 2011 年 12 月 5 日
I tried, but i think it changed to string. can we change it back to numeric?
Walter Roberson
Walter Roberson 2011 年 12 月 5 日
It did not change to string. It changed to cell array. And no, you cannot change it back to numeric array because you have entries of different sizes.
Muammar
Muammar 2011 年 12 月 5 日
Sorry my mistake, not string but cell array. Ok thank you.. Actually i want to find cross-validation, but i have to change data number randomly, not its values. But thank you very much...
Muammar
Walter Roberson
Walter Roberson 2011 年 12 月 5 日
If you want to access a random element from a matrix A, then
p = 1 + floor(numel(A) * rand(1,1));
A(p) is then the random element.
If you want to access a random row from a matrix A, then
p = 1 + floor(size(A,1) * rand(1,1));
A(p,:) is then the random row.
Muammar
Muammar 2011 年 12 月 5 日
Thank you, I will try. I am so sorry if it takes your time.
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 日

0 投票

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 日
if i have A = [ data1; data2], data1 has 2 x 2 matrix, and also for data2. But when i make permutation it could be A = [data2;data1] so i have to call data2 first.
Thank you
Muammar
Walter Roberson
Walter Roberson 2011 年 12 月 5 日
Sorry, I do not understand what you are trying to say here. The part that I do understand has no obvious connection to replacing the 1x1 entry at A(1) with the 2x2 entry [1,1;1,1] or [2,2;2,2] that you were asking about ?
Sean de Wolski
Sean de Wolski 2011 年 12 月 5 日
Walter, I think he wants the result to be the switch the concatention order of data1,data2, though it's very unclear.
Muammar
Muammar 2011 年 12 月 5 日
it is like if A =[data1;data2], data1 = [1 1], data2 = [4 4], now i want to change A=[data1 ; data2] to be A=[1 1;4 4], i change to 1 x 1 matrix.
Thank you
Muammar
Muammar
Muammar 2011 年 12 月 5 日
I am sorry.. Actually i want to make permutation with 2 data, the results could be [data1 data2;data2 data1], when data1 appears i need it to be [1 1;1 1], when data2 it should be [2 2;2 2].. I am so sorry if i make you confuse because i just start using MATLAB.
Thank you
Image Analyst
Image Analyst 2011 年 12 月 5 日
Huh? You didn't change A at all. It's still [data1;data2] which is [1 1;4 4] - no change at all. And what is supposed to be a 1x1 matrix?
Walter Roberson
Walter Roberson 2011 年 12 月 5 日
A=[1 1;4 4] is not a 1 x 1 matrix: it is a 2 x 2 matrix. The only way to make a 1 x 1 matrix as a result would be to use
A = {[1 1;4 4]};
which would create a 1 x 1 cell array.
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 日

0 投票

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

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by