MATLAB Answers

0

how to replace the elements row by rows instead of column by column in matrix

Asked by M.S. Khan on 20 Jul 2019
Latest activity Commented on by M.S. Khan on 23 Jul 2019
A =[ 0 0 3 3 3 0 0 3 0 0; 0 0 0 3 3 3 0 3 3 0]
[rows,colms ] = size(A)
for i = 1:rows
for j = 1:colms
index-1 = find(A==3,1,'first')
index_2 = find(A==3,1,'last')
If A(i,j)=3 & A(i,j)==index_1
A(i,index_1:index_2) = A(i,index_1:index_2) +1
end
end
end
it gives me 5th and 18th indices while i want to get row wise like first should be 3rd and last should be 6th.
please help me in resolving this problem.
warm regards in advance.

  2 Comments

Show how your expected result should look like.
Mr. Madhan Ravi, i want the first 3 in the rows as the lowest element and the last one as the heighest element in each row. and want to add 1 .e.g.
0 0 3 3 3 0 0 3 0 0 => 0 0 4 4 1 1 4 0 0 (i want to add 1 from lowest 3 to highest 3)
0 0 0 3 3 3 0 3 3 0 => 0 0 0 4 4 4 1 4 4 0

Sign in to comment.

3 Answers

Answer by TADA
on 20 Jul 2019
 Accepted Answer

find(A==3,1,'first')
find(A==3,1,'last')
These lines find linear indices not [row, col] subsets. Linear indices go along all the rows of the first column, then on to the second column and so on.
These two lines also disregard i and j completely, so they always give the absolute first and last linear indices in the entire matrix each iteration (5 and 18).
I dont know what exactly you're trying to achieve, but maybe you need to compare only current row:
index_1 = find(A(i,:)==3,1,'first');
index_2 = find(A(i,:)==3,1,'last');
Look here for explanation on array indexing in Matlab
if A(i,j)=3 & A(i,j)==index_1
This condition compares the value of A(i,j) to 3 and to the first index which equals 3, that makes little sence to me, but i may be missing your intent
If you explain with more detail what you are trying to do, we may be able to help you get to the right solution

  5 Comments

Mr. TADA, thanks for all your guidance. God bless you. Warm Regards.
i am very thankful to all community friends who provided me their best feedback.
My special prayers to all for sharing their professional knowlege and cooperation.
Cheers TADA. Heartiest Greetings!!!

Sign in to comment.


Answer by Bruno Luong
on 20 Jul 2019

f = @(A)cumsum(A==3,2)>0;
A = A + f(A).*fliplr(f(fliplr(A)))

  4 Comments

Show 1 older comment
"could you plz explain to me."
Sure here is step by step for single row input:
> A = [0 0 3 3 3 0 0 3 0 0].
This will put 1 at the place where there is element with value == 3, so 0 before the first 3 on the left
>> A==3
ans =
1×10 logical array
0 0 1 1 1 0 0 1 0 0
When I apply cumsum to this, after the first 3 element values of the ouput are >= 1. (it actually increase by 1 when it meets a 3)
>> cumsum(A==3)
ans =
0 0 1 2 3 3 3 4 4 4
I need array of 0s, but then 1s starting from the most left 3, so I do logical commparison
>> cumsum(A==3)>0
ans =
1×10 logical array
0 0 1 1 1 1 1 1 1 1
The three steps are combined, I put in a anonymous function
f = @(A)cumsum(A==3,2)>0;
mask1 = f(A)
Now I want to do the exact same trick but runiing from right-to-left. I simply flip the input array, apply f(), then flip the output back
> mask2 = flip(f(flip(A)))
mask2 =
1×10 logical array
1 1 1 1 1 1 1 1 0 0
Meaning I have array with 0s on the right of the last 3 and 1s on the left.
The product of mask1 and mask2 gives
>> mask1.*mask2
ans =
0 0 1 1 1 1 1 1 0 0
provides array with 0s on the right of the last 3, 0 on the left of the first 3, and 1s in between them.
I then simply add them to the original array A to get the desired result.
Dear Bruno, its so complicated. its blowing above my head.
Thanks for your time.
Regards

Sign in to comment.


Answer by KALYAN ACHARJYA on 20 Jul 2019
Edited by KALYAN ACHARJYA on 20 Jul 2019

A=[0 0 3 3 3 0 0 3 0 0; 0 0 0 3 3 3 0 3 3 0]
[rows colm]=size(A);
B=zeros(rows,colm);
for i=1:rows
B(i,i+2:end-3+i)=1;
end
result=A+B
Command Window:
A =
0 0 3 3 3 0 0 3 0 0
0 0 0 3 3 3 0 3 3 0
result =
0 0 4 4 4 1 1 4 0 0
0 0 0 4 4 4 1 4 4 0
>>

  1 Comment

Thanks Mr. Kalyan for kind contribution and support

Sign in to comment.