Very tricky:Manipulation with complex array
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jovan Jovovic
am 19 Sep. 2022
Bearbeitet: Jovan Jovovic
am 19 Sep. 2022
I need to write a m.file function for this.
For example
array X = [4, 1, 1, 3, 8, 12], I need to make the array which will contain bold ones as well Y = [4, 1, 5, 1, 3, 4, 8, 12, 20].
3 Kommentare
Akzeptierte Antwort
Fabio Freschi
am 19 Sep. 2022
Bearbeitet: Fabio Freschi
am 19 Sep. 2022
% original vector
X = [4, 1, 1, 3, 8, 12];
% reshape to have 2xN vector
W = reshape(X,2,[]);
% calculate the sum
S = sum(W,1);
% concat the vector and reshape
Y = reshape([W; S],1,[])
3 Kommentare
Fabio Freschi
am 19 Sep. 2022
Why using for loops when the vectorized version is effective, cleaner and more efficient?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!