Reputation: 345
I have two tables for some values and want to do a matrix calculation with it by Matlab. I know how to do it in scalar and then make a vector by combining these calculated scalar values. The problem description is as follows:
Table 1
in|out| IMP | Ang
1 | 2 | 0.871 | 0.281
1 | 4 | 0.304 | 0.304
1 | 5 | 0.064 | 0.064
2 | 3 | 0.108 | 0.108
3 | 4 | 0.297 | 0.297
4 | 5 | 0.297 | 0.297
%%%%%%%%%%%%%%%%
Table 2
indx| val | shift
1 | 1.0003 | 0.1
2 | 0.9784 | 0.2
3 | 1.0019 | 0.09
4 | 1.0039 | 0.1
5 | 1.0475 | 0.5
The scalar values are calculated this way. We must get 6 scaler values based 'in' and 'out' at table one. these values are C12,C14,C15,C23,C34,C45, other elements like C35 is 0. where
C12=(1.003/0.871)*(cos(0.281)-0.978*cos(0.281-0.2+0.1)).
In other words,
C12=(val(1)/IMP(1,2))*(cos(Ang(1,2))-val(2)*cos(Ang(1,2)-shift(2)+shift(1))).
I tried to make a matrix using this table. for example I've constructed IMP_mat
for 3rd column of table 1 (IMP_mat(1,2)=0.871
and so on). But in equations for calculating C12,.. I am getting error as some values for IMP_mat
are zero as they are not defined in table (for example IMP_mat(1,3)=0).
%% EDIT:
Based on @krisdestruction's suggestion, I have written the code in table format and converted it in matrix form. But I don't know what to do next.
in=[1 1 1 2 3 4]';
out=[2 4 5 3 4 5]';
IMP=[0.871 0.304 0.064 0.108 0.297 0.297 ]';
Ang=[0.281 0.304 0.064 0.108 0.297 0.297]';
indx=[ 1 2 3 4 5]';
val=[1.0003 0.9784 1.0019 1.0039 1.0475]';
shift=[0.1 0.2 0.09 0.1 0.5]';
T1=table(in,out,IMP,Ang)
T2=table(indx,val,shift)
M1 = table2array( T1 );
M2 = table2array( T2 );
And now, I think the problem description for c12,... becomes more complex.
Also, it seems using this table thing expend a lot of compution. Is that right?
My actual data have thousands of elements.
%% My Attemp:
I have tried to write them manually in matrix forms and do the calculation. But still I don't know how to vectorize my relationship for C.
Here is the script:
%IMP %Ang
IMPmat( 1 , 2 )= 0.871 ; Angmat( 1 , 2 )= 0.281 ;
IMPmat( 1 , 4 )= 0.304 ; Angmat( 1 , 4 )= 0.304 ;
IMPmat( 1 , 5 )= 0.064 ; Angmat( 1 , 5 )= 0.064 ;
IMPmat( 2 , 3 )= 0.108 ; Angmat( 2 , 3 )= 0.108 ;
IMPmat( 3 , 4 )= 0.297 ; Angmat( 3 , 4 )= 0.297 ;
IMPmat( 4 , 5 )= 0.297 ; Angmat( 4 , 5 )= 0.297 ;
%
val=[1.0003 0.9784 1.0019 1.0039 1.0475]';
shift=[0.1 0.2 0.09 0.1 0.5]';
%%
C12=(val(1)/IMPmat(1,2))*(cos(Angmat(1,2))-val(2)*cos(Angmat(1,2)-shift(2)+shift(1)))
C14=(val(1)/IMPmat(1,4))*(cos(Angmat(1,4))-val(4)*cos(Angmat(1,4)-shift(4)+shift(1)))
%% EDIT(New)
I have tried something else. It gives some results without error. But I am not sure if I calculated correctly. But for the first case, it gives the correct result. Here is my Attempt:
in= [1 1 1 2 3 4]';
out=[2 4 5 3 4 5]';
IMP=[0.871 0.304 0.064 0.108 0.297 0.297 ]';
Ang=[0.281 0.304 0.064 0.108 0.297 0.297]';
indx=[ 1 2 3 4 5]';
val=[1.0003 0.9784 1.0019 1.0039 1.0475]';
shift=[0.1 0.2 0.09 0.1 0.5]';
C=(val(in)./IMP).*(cos(Ang)-val(out).*cos(Ang-shift(out)+shift(in)))
Upvotes: 0
Views: 135
Reputation: 1960
It looks like you're trying to do specific operations that can be easily done/vectorized in a matrix. In that case, I would recommend you convert it into a matrix using table2mat
first and perform your calculation. It's easy because you have all numeric data.
Suppose you have a table formatted like this.
Var1 Var2 Var3 Var4
____ ____ ____ ____
1 2 2 1
1 4 5 5
1 5 1 3
2 3 3 7
3 4 5 5
4 5 5 5
The code to generate is below.
T = table([1 1 1 2 3 4]',[2 4 5 3 4 5]',[2 5 1 3 5 5]',[1 5 3 7 5 5]')
You can use this line of code to convert it to a matrix array.
M = table2array( T );
You can then proceed to perform your calculations using matrices and for each of the C12
,C14
,C15
,C23
,C34
,C45
in vector form (if possible).
Then you can use the following line of code to do your calculation.
C = val(in) ./ IMP .* cos( Ang(out) ) - val(out) .* cos( Ang(in) - shift(out) + shift(in) );
The result is shown here.
>> C
C =
2.1772
8.2435
2.1805
14.3389
8.2204
2.2290
Upvotes: 1