Make specific array from another array elements

22 views (last 30 days)
The problem:
''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]
  2 Comments
Jan
Jan on 19 Sep 2022
This sounds like a homework question. It would not be useful, if the forum solves your homework, so you will not get a solution here. Post, what you have tried so far, and ask a specific question about Matlab. Then you will get assistence for solving the specific problem.
Jovan Jovovic
Jovan Jovovic on 19 Sep 2022
The problem is that its exam question.And i have exam in 2 days.I learn best from example,because i can't find solution how to make subarray from the array.However i got algorithm of the solution in the head,i just don't know to write it properly.
From my knowlege i would do it smh like this.
x=input('input array X');
y=[]
s=0
for i=1:length(x)
while k<0
k=(i)
d=rem(k,10)
if rem(d,2)==0
s=s+d
k=fix(k,10)
y=[k]
I know its not working,but logic of my solution is.
We took first element of the array x.
There is a k that will have same value.There is a d which will be our detector of the digit ,if that number can be divide with 2 ,it means that its even,so s(sum) will be s+that number.After that we need to take a look over next digit of the first element.We are doing that by shorting fix/floor function,,and doing it until the we get to the zero.
When we got the zero,we are moving to next element.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 19 Sep 2022
Y = zeros(size(X));
for IndexBackM7 = 1 : numel(Y)
ValueToWorkWIthBackM7 = X(IndexBackM7);
now calculate for ValueToWorkWIthBackM7
Y(idx) = ResultOfCalculationBackM7
end

Community Treasure Hunt

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

Start Hunting!