Subtract each column of matrix until -3 is finished

For example, i have a matrix a = [0 1 2 3 4]. How do I subtract each column by 1?
a = [0 3 2 6 4]
b = [0 2 1 2 3]
remainder = size(a, 2) - sum(b)
The remainder is -3. I need to minus from b when b(i) ~= 0 so that at the end the b = [0 1 0 1 3].
Thank you.

 Accepted Answer

I am curious too. Let me guess.
a=0:4;
b=-3; % b is an integer, positive or negative
c=zeros(size(a));
ind=find(a,1);
c(ind:ind+abs(b)-1)=-1;
a=a+c
a =
0 0 1 2 4
Updated for a generic solution
b = [0 2 1 2 3]
remainder=-6;
ind=find(b);
while remainder<0 && ~isempty(ind)
if length(ind)<=abs(remainder)
b(ind)=b(ind)-1
remainder=remainder+length(ind)
ind=find(b);
else
for k=1:abs(remainder)
b(ind(k))=b(ind(k))-1
remainder=remainder+1
end
end
end
b =
0 2 1 2 3
b =
0 1 0 1 2
remainder =
-2
b =
0 0 0 1 2
remainder =
-1
b =
0 0 0 0 2
remainder =
0

9 Comments

Actually I have a remainder = 3 and I would like reduce the remainder from the matrix column one by one so that at the end the the remainder is 0.
What is "remainder"? What will you do if you do it manually, like solve it with a pencil and paper?
I already edit the question and your answer is accepted but I am facing one more problem. if now the remainder = -6 then how do i get the result so that now b = [0 0 0 0 2].
For example,
b = [0 2 1 2 3]
when b(i) ~= 0 then
b = [0 2 1 2 3]
-1 -1 -1 -1
b = [0 1 0 1 2]
-1 -1
so total is minus 6 times. Is this more clear.
For that, you can use the while-loop. See updated answer.
I would like to know if this time I am using c = [0 1.2 2.3 0 0.4] and i would like to subtract the remainder from a based on the c matrix from the smallest number to the largest number.
For example,
c = [0 1.2 2.3 0 0.4]
remainder = -3
b = [0 2 1 2 3]
final answer is
b = [0 1 1 1 2]
then the remainder will subtract from b(4) first, second is b(5) and the final is b(2). If c(1)=0 and the b(1)=0 then ignore. how do I get the correct indexing? Thank you.
I already done a code
a = [0 1.2 2.3 0 0.4];
d = [0 2 1 2 3];
remainder = -3;
for i = 1 : size(d, 2)
c(i) = i;
end
[row col] = sort(a, 'ascend');
for j = 1 : size(col, 2)
if a(col(j)) && d(c(j)) ~= 0
col = col(1 : abs(remainder));
d(col) = d(col) - 1
end
end
but I getting error message and also cannot get the b=[0 1 1 1 2]. What I am missing?
I am confused with the c and a. I suggest you ask a separate question and use variable name consistently. Also, explain the logic or step. If it's hard to explain, try to show your step and intermediate result if you do it manually, like using a pencil and a piece of paper.
ok. thanks. i will ask in another question.

Sign in to comment.

More Answers (1)

Does the minus sign of -3 indicate the third from the start or the third from the end? Your example is ambiguous about that.
Third from the start:
a(1:abs(b)) = a(1:abs(b)) - 1;
Third from the end:
a(1:end-abs(b)+1) = a(1:end-abs(b)+1) - 1;

2 Comments

The result are all wrong though!
ind = find(a);
ind = ind(1:abs(b));
a(ind) = a(ind) - 1;

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!