How to transform a matrix in Matlab?

38 次查看(过去 30 天)
Moe
Moe 2016-2-29
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?

请先登录,再进行评论。

类别

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