Hello, I am new to matlab and in need of help. I have a 43200 x 2 matrix and having difficulty writting a code for this equation: Y=A+(D+C/2)*1, Z=Y-B. where A is -120449852, D=3311.5705, C=1476.9455, B=-10749852 from the data shown below.Please how do i code this in such a way that it repeats this operation for all the elements in the matrix sequentially and saves Z values for each iteration. for example, the next vaule of A=-107496428, D=1476.9455, C=-1719.7385 and B=-117996722 and so on...
Please note that data sample shown below is for first epoch,each epoch ends with zeros at the bottom, kindly bear this in mind while coding. Thanks
Data
-120449852 3311.5705
-107496428 1476.9455
-117996722 -1719.7385
-120627169 -1228.7064
-116870191 -3087.5875
-134480051 -125.6097
-125355131 3313.2842
-126744025 994.96613
-112248639 937.16468
-130631487 -3113.0687
-108469847 -191.42345
0 0

 採用された回答

Stephen23
Stephen23 2021 年 1 月 7 日

0 投票

I don't see why any iterations are required, vectorized code will do this quite easily:
A = [-120449852, -107496428];
B = [-10749852, -117996722];
C = [1476.9455, -1719.7385];
D = [3311.5705, 1476.9455];
Y = A+(D+C/2)*1
Y = 1×2
-1.2045 -1.0750
Z = Y-B
Z = 1×2
-1.0970 0.1050

5 件のコメント

JULIET OSUNDE
JULIET OSUNDE 2021 年 1 月 7 日
hello Stephen,thanks for responding but the data i'm using is 43200x2 matrix, for the first iteration, A is the value in the first row and first colunm(-120449852), B is value in the secound row and first column(-10749852), C is value in row 1 of column 2(3311.5705 ) and D is value in row 2 of colum 2(1476.9455). I need the operation repeated for all elements in the matrix (43200 x 2) and the vaule of Z printed for each iteration. the data shown is a sample for first epoch, there are a total of 3,600 epochs in the data which adds up to give a 43200 x 2 matrix.
Stephen23
Stephen23 2021 年 1 月 7 日
編集済み: Stephen23 2021 年 1 月 7 日
"A is the value in the first row and first colunm(-120449852), B is value in the secound row and first column(-10749852), C is value in row 1 of column 2(3311.5705 ) and D is value in row 2 of colum 2(1476.9455)."
In your original question you wrote "D=3311.5705, C=1476.9455, B=-10749852", which means either your question or your comment must be incorrect (or perhaps both):
  • is D = 3311.5705 as you wrote in your question, or D = 1476.9455 as your wrote in your comment?
  • is C = 1476.9455 as your wrote in your question, or C = 3311.5705 as your wrote in your comment?
If I look at your question, the values you gave for the second block of A/B/C/D values also seems to be incorrect. Following the pattern you described in your previous comment (i.e. A=(1,1), B=(2,1), C=(1,2), D=(2,2)), then I would expect the second D=-1228.7064, but in your description you gave it as D=1476.9455, which seems like it should be D from the first block (but was given as the C value).
Which means so far you have described/given three different patterns for how the data is arranged.
In any case, I still do not see why you need any iterations: just rearrange the data into something more sensible, then use basic indexing to access the A/B/C/D columns (or rows, whichever you prefer) and enter them into your calculation.
JULIET OSUNDE
JULIET OSUNDE 2021 年 1 月 7 日
oh my bad, i apologize for the mix up. this is what i meant to say, for the first block, A=(1,1), B(2,1), C=(1,2), D=(2,2). for second block, A=(2,1), B=(3,1), C=(2,2), D=(3,2) and so on.... thanks
Stephen23
Stephen23 2021 年 1 月 7 日
M = [...
-120449852 3311.5705
-107496428 1476.9455
-117996722 -1719.7385
-120627169 -1228.7064
-116870191 -3087.5875
-134480051 -125.6097
-125355131 3313.2842
-126744025 994.96613
-112248639 937.16468
-130631487 -3113.0687
-108469847 -191.42345
0 0];
A = M(1:end-1,1);
B = M(2:end ,1);
C = M(1:end-1,2);
D = M(2:end ,2);
Y = A+(D+C/2)*1
Y = 11×1
-1.2045 -1.0750 -1.1800 -1.2063 -1.1687 -1.3448 -1.2535 -1.2674 -1.1225 -1.3063
Z = Y-B
Z = 11×1
-0.1295 0.1050 0.0263 -0.0376 0.1761 -0.0912 0.0139 -0.1449 0.1838 -0.2216
JULIET OSUNDE
JULIET OSUNDE 2021 年 1 月 7 日
Thank you Stephen, It worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by