How to do multiple divisions in order to generate another array?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Brenno Selli
le 27 Fév 2023
Modifié(e) : Stephen23
le 27 Fév 2023
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??
0 commentaires
Réponse acceptée
Steven Lord
le 27 Fév 2023
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
m = reshape(x, 4, 3) % Taking sum of squares of blocks of 4 elements
s = sum(m.^2, 1)
Let's spot check the answer for the second block of 4.
check = 5.^2+6.^2+7.^2+8.^2
3 commentaires
Stephen23
le 27 Fév 2023
Modifié(e) : Stephen23
le 27 Fév 2023
"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)
m = reshape(yourvector(2:end),4000,[]).';
sqrt(sum(m.^2,2)./4000)
Checking the first result using your calculation:
sqrt(sumsqr(yourvector(2:4001,1))/4000)
Note that YOURVECTOR has not changed at all.
Steven Lord
le 27 Fév 2023
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)
x remains unchanged.
x
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!