How to transform a matrix in Matlab?
이전 댓글 표시
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
Walter Roberson
2016년 2월 29일
I cannot figure out why it is the last 250 for ID 2 that is getting the 1 and not the first 250. Everything else seems to work out for me. I think the output should be
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];
John BG
2016년 2월 29일
Walter
reading a similar question asked by Mohammad earlier on may help to understand
A is used as a table.
There is already an answer to similar question, stacking the binary lines rather than lining them up horizontally.
Moe
2016년 2월 29일
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
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
2016년 2월 29일
Mohammad Hesam comments
This is not "linsolve" problem.
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
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]];
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!