How to do multiple divisions in order to generate another array?

2 ビュー (過去 30 日間)
Brenno Selli
Brenno Selli 2023 年 2 月 27 日
編集済み: Stephen23 2023 年 2 月 27 日
I have this 60001x1 array called "assust", I need it to be compressed in order to obtain certain values, a few months ago I just repeat the line 15 times but now I have to make it work better and automatically
The code is:
A(1) = sqrt(sumsqr(asust1(2:4001,1))/4000);
A(2) = sqrt(sumsqr(asust1(4001:8000,1))/4000);
A(3) = sqrt(sumsqr(asust1(8001:12000,1))/4000);
A(4) = sqrt(sumsqr(asust1(12001:16000,1))/4000);
A(5) = sqrt(sumsqr(asust1(16001:20000,1))/4000);
A(6) = sqrt(sumsqr(asust1(20001:24000,1))/4000);
A(7) = sqrt(sumsqr(asust1(24001:28000,1))/4000);
A(8) = sqrt(sumsqr(asust1(28001:32000,1))/4000);
A(9) = sqrt(sumsqr(asust1(32001:36000,1))/4000);
A(10) = sqrt(sumsqr(asust1(36001:40000,1))/4000);
A(11) = sqrt(sumsqr(asust1(40001:44000,1))/4000);
A(12) = sqrt(sumsqr(asust1(44001:48000,1))/4000);
A(13) = sqrt(sumsqr(asust1(48001:52000,1))/4000);
A(14) = sqrt(sumsqr(asust1(52001:56000,1))/4000);
A(15) = sqrt(sumsqr(asust1(56001:60001,1))/4000);
Is there any way that I'm able to do this automatically??

採用された回答

Steven Lord
Steven Lord 2023 年 2 月 27 日
All but two of your lines follow a pattern. I've commented the relevant lines out here so I can run code later in my answer.
%{
A(1) = sqrt(sumsqr(asust1(2:4001,1))/4000);
A(2) = sqrt(sumsqr(asust1(4001:8000,1))/4000);
%snip
A(15) = sqrt(sumsqr(asust1(56001:60001,1))/4000);
%}
Are you sure you wanted the overlap between A(1) and A(2)? Are you sure you want to include the extra element in A(15)?
If you want to operate on blocks of 4000 elements, instead I'd reshape asust1 into a 4000-by-N array and operate down the columns. Assuming your sumsqr function computes the sum of squares of the elements, this is fairly straightforward with the element-wise power operator (.^) and the dim input to the sum function.
x = 1:12
x = 1×12
1 2 3 4 5 6 7 8 9 10 11 12
m = reshape(x, 4, 3) % Taking sum of squares of blocks of 4 elements
m = 4×3
1 5 9 2 6 10 3 7 11 4 8 12
s = sum(m.^2, 1)
s = 1×3
30 174 446
Let's spot check the answer for the second block of 4.
check = 5.^2+6.^2+7.^2+8.^2
check = 174
  3 件のコメント
Stephen23
Stephen23 2023 年 2 月 27 日
編集済み: Stephen23 2023 年 2 月 27 日
"Anyway, I still need "assust1" to stay in the 60001x1 format..."
Steven Lord's code does not require that you change your vector (but you could if you wanted to).
Look at the example: x is the input vector (its orientation is irrelevant) and m is the matrix that results from reshaping. So x remains a vector, exactly as it was at the start. No change at all to x. The matrix m allows operations to be performed along its rows or columns. Performing calculations along matrix rows or columns is very very common in MATLAB, it is one of MATLAB's superpowers:
"My initial idea was to generate an array called "A" in 15x1 format where each one of the lines is the result of the operation described in the code I sent...."
Lets try it now, follwing the same assumption that you really want blocks of 4000 elements:
yourvector = rand(60001,1)
yourvector = 60001×1
0.8714 0.6501 0.0588 0.4090 0.8544 0.8898 0.4278 0.9750 0.4727 0.5034
m = reshape(yourvector(2:end),4000,[]).';
sqrt(sum(m.^2,2)./4000)
ans = 15×1
0.5753 0.5743 0.5778 0.5823 0.5812 0.5799 0.5769 0.5775 0.5716 0.5716
Checking the first result using your calculation:
sqrt(sumsqr(yourvector(2:4001,1))/4000)
ans = 0.5753
Note that YOURVECTOR has not changed at all.
Steven Lord
Steven Lord 2023 年 2 月 27 日
Note that my creating of m in the example code did not do anything to change x. In fact I don't need to explicitly create m as a named variable if it's intended to be temporary.
x = 1:12;
s = sum(reshape(x, 4, 3).^2, 1)
s = 1×3
30 174 446
x remains unchanged.
x
x = 1×12
1 2 3 4 5 6 7 8 9 10 11 12

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by