Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

matrix manipulation

2 views
Skip to first unread message

dhruv sharma

unread,
May 8, 2009, 7:32:01 AM5/8/09
to
Hello:

I would really appreciate help for doing the following manipulation. I have a matrix A. It has non-zero numbers in each row followed by 0s.
A = [0.1, 0.2 ,0;
0.2, 0, 0;
0.3, 0.4, 0]

I want to convert this matrix to a another matrix B, such that each element before the first occurrence of 0 is subtracted by 1. So B looks like:
A = [0.1,0.8,0;
0.8,0,0;
0.3,0.7,0]

Thanks a lot for your help. Dhruv.

Jean-Christophe

unread,
May 8, 2009, 7:50:03 AM5/8/09
to

Since the element to be substraced from 1 is not always at
the same position in the matrix, and since you need to check
that the following element has a value of zero,
I think that you will have to use a FOR loop.

us

unread,
May 8, 2009, 7:55:03 AM5/8/09
to
"dhruv sharma" <stnfr...@yahoo.com> wrote in message <gu157h$stq$1...@fred.mathworks.com>...

one of the many solutions

% the data
m=[
0.1 0.2 0
0.2 0 0
0.3 0.4 0
];
% the engine
[r,c]=find(m==0);
rc=sortrows([r,c]);
ix=[true;diff(rc(:,1))~=0];
lx=sub2ind(size(m),rc(ix,1),rc(ix,2)-1);
lx=lx(lx>0);
m(lx)=1-m(lx);
% the result
disp(m);
%{
0.1 0.8 0
0.8 0 0
0.3 0.6 0
%}

us

Jos

unread,
May 8, 2009, 8:58:15 AM5/8/09
to
"dhruv sharma" <stnfr...@yahoo.com> wrote in message <gu157h$stq$1...@fred.mathworks.com>...

0.7 ??

and what if there is no first zero?

Here is something that comes close:

q = fliplr(cumsum(fliplr(~~A),2)) == 1 & A~=0
A(q) = 1 - A(q)

which was inspired by this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/250469

Jos

dhruvn...@gmail.com

unread,
May 8, 2009, 1:54:34 PM5/8/09
to
On May 8, 5:58 am, "Jos " <#10...@fileexchange.com> wrote:
> "dhruv sharma" <stnfrd1...@yahoo.com> wrote in message <gu157h$st...@fred.mathworks.com>...

Thanks a lot for your help. Both solutions work. But my problem has
now become 3D. So if
A(:,:,1) = [0.1, 0.2, 0;
0.2, 0, 0;
0.6, 0.4, 0]

A(:,:,2) = [0.2, 0.7, 0;
0.3, 0.8, 0;
0.1, 0, 0]
The changed matrix will be:
B(:,:,1) = [0.1, 0.8, 0;
0.8, 0, 0;
0.4, 0.6, 0]
B(:,:,2) = [0.2,0.3,0;
0.3, 0.2, 0;
0.9, 0, 0]

There is always a 0 at the end of a row.

Matt

unread,
May 8, 2009, 4:14:02 PM5/8/09
to
dhruvn...@gmail.com wrote in message <739c8cb6-4e22-4e27...@n7g2000prc.googlegroups.com>...

> Thanks a lot for your help. Both solutions work. But my problem has
> now become 3D. So if
> A(:,:,1) = [0.1, 0.2, 0;
> 0.2, 0, 0;
> 0.6, 0.4, 0]
>
> A(:,:,2) = [0.2, 0.7, 0;
> 0.3, 0.8, 0;
> 0.1, 0, 0]
> The changed matrix will be:
> B(:,:,1) = [0.1, 0.8, 0;
> 0.8, 0, 0;
> 0.4, 0.6, 0]
> B(:,:,2) = [0.2,0.3,0;
> 0.3, 0.2, 0;
> 0.9, 0, 0]
>

I think you have a typo in element B(3,1,1). I think you want B(3,1,1)=A(3,1,1)=0.6

If this is so, then I propose the following

K=[-1 1];
Mask=convn(logical(A),K,'same')>0;

B=A;
B(Mask)=1-B(Mask),

dhruv sharma

unread,
May 8, 2009, 4:49:02 PM5/8/09
to
Matt you are right, I had a typo. Great! Matt's solution is working. Thanks for all your comments and help - this has saved me several hours of thinking how to do this. Dhruv.

"Matt " <x...@whatever.com> wrote in message <gu23qa$9q7$1...@fred.mathworks.com>...

0 new messages