How to transform a matrix in Matlab?

조회 수: 77 (최근 30일)
Moe
Moe 2016년 2월 29일
댓글: Priti Gujar 2020년 6월 15일
Matrix A is as follows:
A = [0 240 245 250
25 1 2 1
63 3 2 1];
I want matrix A to be transformed to B (like follows):
B = [0 240 245 250 240 245 250 240 245 250
25 1 0 1 0 1 0 0 0 0
63 0 0 1 0 1 0 1 0 0];
there are three different variables in matrix A, so, 204 to 250 (first row) in matrix B is repeated 3 times (e.g. if there were 5 variables, then 240 to 250 should be repeated 5 times). Then, value of ID = 25 has 1, so 1 is added to B(2,2). Again, A(2,3) = 2, then B(2,6) should by =1 and A(2,4)=1, then A(2,10) should be equal by =1. And same for ID#63
  댓글 수: 4
Moe
Moe 2016년 2월 29일
Hi Walter,
You are right, the output should be same as you wrote in your comment. I edited in main question. Do you have any idea how to get this result?
Priti Gujar
Priti Gujar 2020년 6월 15일
Use the readall function to import all the data. Check that the preprocessing function was applied to each file by plotting the Y variable as a function of Time.

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

답변 (2개)

John BG
John BG 2016년 2월 29일
Hi Mohammad
MATLAB has the command linsolve to solve linear equation systems of the type A*x=b
Your question has one A:
A = [0 240 245 250; 25 1 2 1; 63 3 2 1]
and ten b:
B= [0 240 245 250 240 245 250 240 245 250;
25 1 0 0 0 1 0 0 0 1;
63 0 0 1 0 1 0 1 0 0]
One way to solve them is with a for loop:
C=zeros(4,10)
for k=1:1:10
C(:,k)=linsolve(A,B(:,k))
end
answer:
C =
Columns 1 through 6
1.00 0.00 -0.04 -0.04 -0.04 0.00
-0.00 -0.55 0.77 1.30 0.75 -0.02
0 0 0 0 0 0
0.00 1.49 0.24 -0.25 0.24 1.00
Columns 7 through 10
-0.04 -0.04 -0.04 0.00
0.78 1.27 0.77 -0.52
0 0 0 0
0.25 -0.26 0.24 1.50
test it's the correct answer
A*C
ans =
Columns 1 through 6
0.00 240.00 245.00 250.00 240.00 245.00
25.00 1.00 -0.00 -0.00 -0.00 1.00
63.00 -0.00 0 1.00 0 1.00
Columns 7 through 10
250.00 240.00 245.00 250.00
0 -0.00 -0.00 1.00
0 1.00 0 -0.00
note the type (class) has changed to double. To bring it back to, for instance, range [0 255] use uint8.
does this answer help? if so click on the thumbs-up icon link on the top of this page, thanks in advance
John
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 2월 29일
Mohammad Hesam comments
This is not "linsolve" problem.
John BG
John BG 2016년 2월 29일
understood, had to read the previous question to realize that A is a table and the kind of the variable tagging sought.
B has to be built by the answer, not used by it to find a transform matrix.
Thanks for mentioning Mohammad's comment.

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


Andrei Bobrov
Andrei Bobrov 2016년 2월 29일
편집: Andrei Bobrov 2016년 2월 29일
[m,n] = size(A);
[ii,k] = ndgrid(1:n-1,1:m-1);
jj = A(2:end,2:end)';
b0 = accumarray([ii(:),jj(:),k(:)],1,[n-1,n-1,m-1]);
B = [nan,repmat(A(1,2:end),1,n-1);[A(2:end,1),reshape(b0,[],m-1)']];
or with bsxfun
[m,n] = size(A);
n1 = n - 1;
A0 = A(2:end,2:end)';
b0 = reshape( bsxfun(@eq,1:n1,reshape(A0,n1,[],m-1)),[],m-1)';
B = [nan,repmat(A(1,2:end),1,n-1);[A(2:end,1),b0]];
  댓글 수: 2
Moe
Moe 2016년 2월 29일
Thanks Andrei,
The output of your code is:
B = [NaN 240 245 250 240 245 250 240 245 250
25 1 0 0 0 1 1 0 0 1
63 1 0 0 0 1 0 0 0 0];
While it's different with the output that I wanted in main question. Can you edit your code?
Andrei Bobrov
Andrei Bobrov 2016년 2월 29일
corrected

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

카테고리

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