How to transform a matrix in Matlab?
27 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Priti Gujar
am 15 Jun. 2020
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.
Antworten (2)
John BG
am 29 Feb. 2016
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 Kommentare
John BG
am 29 Feb. 2016
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
am 29 Feb. 2016
Bearbeitet: Andrei Bobrov
am 29 Feb. 2016
[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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!