Hi Everyone
I have a matrix of the following form:
P=
2 3 0 0 0 0 0
2 6 3 0 0 0 0
2 5 3 0 0 0 0
2 1 4 5 6 7 3
I want to remove the zeros in all the rows to get something like this:
2 3
2 6 3
2 5 3
2 1 4 5 6 7 3
I tried the following:
P(P == 0) = []
but i got the wrong output:
Columns 1 through 14
2 2 2 2 3 6 5 1 3 3 4
5 6 7
Column 15
3
The command removes zeros but, converts rows to columns. However i
need the output as mentioned above.
Any guidance/help in this regard is highly appreciated.
Best Regards
|
|
0
|
|
|
|
Reply
|
Virtualized
|
11/29/2010 3:58:59 PM |
|
Virtualized <razzaqadil@gmail.com> wrote in message <afa3c58d-37e6-425c-b7cc-66ac5f2bb705@l8g2000yqh.googlegroups.com>...
>
> I want to remove the zeros in all the rows to get something like this:
>
> 2 3
> 2 6 3
> 2 5 3
> 2 1 4 5 6 7 3
>
================
This is a non-rectangular array of numbers, so you cannot call it a "matrix" in any strict sense, or contain it as a MATLAB matrix data type
If you're only interested in _displaying_ the matrix with the zeros removed, you can do something like in this example :
>> a = [10 11; 0 1]; regexprep( evalc('a'), '\W0[ ]*', '')
ans =
a =
10 11
1
|
|
0
|
|
|
|
Reply
|
Matt
|
11/29/2010 4:09:07 PM
|
|
On 29/11/10 9:58 AM, Virtualized wrote:
> I have a matrix of the following form:
>
> P=
>
> 2 3 0 0 0 0 0
> 2 6 3 0 0 0 0
> 2 5 3 0 0 0 0
> 2 1 4 5 6 7 3
>
> I want to remove the zeros in all the rows to get something like this:
>
> 2 3
> 2 6 3
> 2 5 3
> 2 1 4 5 6 7 3
You cannot do that with a numeric matrix. All rows in a numeric matrix
must be exactly the same size.
You can create a character array that would _look_ like what you wanted
(but would have a bunch of trailing spaces on lines.)
|
|
0
|
|
|
|
Reply
|
Walter
|
11/29/2010 4:14:58 PM
|
|
As stated above, you cannot contain a non rectangular data in a single matrix. So the trick is to use the string functions (sprintf and str2num) to convert back and forth the data.
Try this:
%%%%%%%%%
for r=1:size(P,1)
str2num(sprintf('%d',P(r,P(r,:)>0)))
end
%%%%%%%%%
Abbas
|
|
0
|
|
|
|
Reply
|
Abbas
|
11/29/2010 4:38:03 PM
|
|
Hi
Matt, Walter and Abbas
Thank you very much
Best Regards
|
|
0
|
|
|
|
Reply
|
Virtualized
|
11/29/2010 5:05:45 PM
|
|
Hi
When i used the statement:
regexprep( evalc('P'), '\W0[ ]*', '')
Now i get the following result:
pathnew =
2 3 0 0
2 6 3 0 0
2 5 3 0 0
2 1 4 5 6 7 3
There are still a few zeros at the end of first 3 lines, while some of
the zeros previously present have been removed.
Can you please guide how to get the result as mentioned above.
Best Regards
|
|
0
|
|
|
|
Reply
|
Virtualized
|
11/29/2010 5:12:45 PM
|
|
On 10-11-29 11:12 AM, Virtualized wrote:
> When i used the statement:
>
> regexprep( evalc('P'), '\W0[ ]*', '')
>
> Now i get the following result:
>
> pathnew =
>
> 2 3 0 0
> 2 6 3 0 0
> 2 5 3 0 0
> 2 1 4 5 6 7 3
>
> There are still a few zeros at the end of first 3 lines, while some of
> the zeros previously present have been removed.
>
> Can you please guide how to get the result as mentioned above.
Change the pattern to '( +0\W)+$'
|
|
0
|
|
|
|
Reply
|
Walter
|
11/29/2010 8:31:56 PM
|
|
Virtualized <razzaqadil@gmail.com> wrote in message <cfe4687d-3128-4698-afed-acdc57520580@a17g2000yql.googlegroups.com>...
>
> There are still a few zeros at the end of first 3 lines, while some of
> the zeros previously present have been removed.
>
> Can you please guide how to get the result as mentioned above.
===========
This seems to work better
>> regexprep( evalc('P'), '\D0', ' ')
ans =
P =
2 3
2 6 3
2 5 3
2 1 4 5 6 7 3
|
|
0
|
|
|
|
Reply
|
Matt
|
11/29/2010 8:33:29 PM
|
|
Another possibility:
>> P
P =
2 3 0 0 0 0 0
2 6 3 0 0 0 0
2 5 3 0 0 0 0
2 1 4 5 6 7 3
>> regexprep( evalc('P'), '\s0', ' ')
ans =
P =
2 3
2 6 3
2 5 3
2 1 4 5 6 7 3
Or, another:
S = sprintf([repmat('%i ',1,size(P,2)),'\n'],P.');
S = regexprep(S, '\s0', ' ')
S =
2 3
2 6 3
2 5 3
2 1 4 5 6 7 3
|
|
0
|
|
|
|
Reply
|
Matt
|
11/29/2010 10:16:21 PM
|
|
Hi Everyone
Thanks a lot.
Best Regards
|
|
0
|
|
|
|
Reply
|
Virtualized
|
11/30/2010 3:50:45 PM
|
|
|
9 Replies
574 Views
(page loaded in 0.15 seconds)
|