필터 지우기
필터 지우기

Very tricky:Manipulation with complex array

조회 수: 2 (최근 30일)
Jovan Jovovic
Jovan Jovovic 2022년 9월 19일
편집: Jovan Jovovic 2022년 9월 19일
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
Jovan Jovovic
Jovan Jovovic 2022년 9월 19일
I was thinking to make 3 sub arrays,one array will go with even(2:2:end),one will go with odd(1:2:end),and one will be sum of first element of the both arrays.
After that i would combine it first element of each array,than second element of each array.
The problem is that i need it to make in a m.file with kinda a algorithm method.
However i will try to make it with function reshape.
If you have some additional advice i would be thankful
Jovan Jovovic
Jovan Jovovic 2022년 9월 19일
Thank you tho

댓글을 달려면 로그인하십시오.

채택된 답변

Fabio Freschi
Fabio Freschi 2022년 9월 19일
편집: Fabio Freschi 2022년 9월 19일
Following @Paul suggestion
% 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,[])
Y = 1×9
4 1 5 1 3 4 8 12 20
  댓글 수: 3
Fabio Freschi
Fabio Freschi 2022년 9월 19일
Why using for loops when the vectorized version is effective, cleaner and more efficient?
Jovan Jovovic
Jovan Jovovic 2022년 9월 19일
편집: Jovan Jovovic 2022년 9월 19일
Because this tasks want from me to learn logic of the Matlab.I know its absolutely better like this.
But maybe i will get problem like this and i will be stucked if i dont know some function.
Write a m.file which is asking to enter array X (integer numbers),which will make a new array Y .Elements of the array Y are sum from even digits of elements array X. If some element of array X is not inteneger,make a comment about error.
For example X = [81, 19, 102, 22, 12, 21] Y = [8, 0, 2, 4, 2, 2]
And I will need to use the loop to see is there a even digit in every number

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by