Dear All, Is there a neat way of getting from, say,
[3, 5, 8]
to
[0, 0, 1, 0, 1, 0, 0, 1]?
|
|
0
|
|
|
|
Reply
|
james
|
3/30/2010 8:57:05 PM |
|
james bejon wrote:
> Dear All, Is there a neat way of getting from, say,
>
> [3, 5, 8]
>
> to
>
> [0, 0, 1, 0, 1, 0, 0, 1]?
B = false(1,max(A));
B(A) = true;
|
|
0
|
|
|
|
Reply
|
Walter
|
3/30/2010 9:07:45 PM
|
|
x = [3 5 8];
y = zeros(1, max(x));
y(x) = 1
|
|
0
|
|
|
|
Reply
|
David
|
3/30/2010 9:09:06 PM
|
|
Oops I didn't look at the subject line, just the example. Walter's answer is the right one, ignore mine.
|
|
0
|
|
|
|
Reply
|
David
|
3/30/2010 9:12:05 PM
|
|
Walter Roberson wrote:
> james bejon wrote:
>> Dear All, Is there a neat way of getting from, say,
>>
>> [3, 5, 8]
>>
>> to
>>
>> [0, 0, 1, 0, 1, 0, 0, 1]?
>
> B = false(1,max(A));
> B(A) = true;
Additional note: if there can be big gaps between the true values, then you
may wish to consider using sparse arrays. Sparse arrays are supported for
values of class double, and values of class logical, but (not as yet) for any
other class.
|
|
0
|
|
|
|
Reply
|
Walter
|
3/30/2010 9:20:47 PM
|
|
james bejon wrote:
> Dear All, Is there a neat way of getting from, say,
>
> [3, 5, 8]
>
> to
>
> [0, 0, 1, 0, 1, 0, 0, 1]?
Alternately,
o(x)=1; % create the 0/1 vector
l = logical(o); % turn it into a logical one
Too bad for things like this ML doesn't support nested assignment..
l = logical(o(x)=1); % Doesn't work, of course... :(
--
|
|
0
|
|
|
|
Reply
|
dpb
|
3/30/2010 9:37:39 PM
|
|
On Mar 30, 2:37=A0pm, dpb <n...@non.net> wrote:
> james bejon wrote:
> > Dear All, =A0Is there a neat way of getting from, say,
>
> > [3, 5, 8]
>
> > to
>
> > [0, 0, 1, 0, 1, 0, 0, 1]?
>
> Alternately,
>
> o(x)=3D1; =A0 =A0 =A0 =A0 =A0% create the 0/1 vector
> l =3D logical(o); =A0% turn it into a logical one
>
> Too bad for things like this ML doesn't support nested assignment..
>
> l =3D logical(o(x)=3D1); =A0 % Doesn't work, of course... :(
>
> --
If you were going to do that, why not just make it logical in the
first place?
o(x) =3D true;
-Nathan
|
|
0
|
|
|
|
Reply
|
Nathan
|
3/30/2010 9:43:30 PM
|
|
Nathan wrote:
....
> If you were going to do that, why not just make it logical in the
> first place?
>
> o(x) = true;
Primarily because...
>> which true
true not found.
>> help true
true.m not found.
>>
:(
--
|
|
0
|
|
|
|
Reply
|
dpb
|
3/30/2010 9:51:56 PM
|
|
dpb wrote:
> james bejon wrote:
>> Dear All, Is there a neat way of getting from, say,
>>
>> [3, 5, 8]
>>
>> to
>>
>> [0, 0, 1, 0, 1, 0, 0, 1]?
>
> Alternately,
>
> o(x)=1; % create the 0/1 vector
> l = logical(o); % turn it into a logical one
>
> Too bad for things like this ML doesn't support nested assignment..
>
> l = logical(o(x)=1); % Doesn't work, of course... :(
Well, if you are going to be like that!
l = any(bsxfun(@eq,1:max(x),x.'));
|
|
0
|
|
|
|
Reply
|
Walter
|
3/30/2010 9:54:47 PM
|
|
Thanks for all the really helpful replies!
I'm currently toying with the (perhaps hungry?) expression:
ismember((1:A(end)), A)
(I can assume A is in ascending order)
|
|
0
|
|
|
|
Reply
|
james
|
3/30/2010 10:08:05 PM
|
|
dpb <none@non.net> wrote in message <hotrs9$qbg$1@news.eternal-september.org>...
> Nathan wrote:
> ...
>
> > If you were going to do that, why not just make it logical in the
> > first place?
> >
> > o(x) = true;
>
> Primarily because...
>
> >> which true
> true not found.
> >> help true
>
> true.m not found.
>
> >>
>
> :(
>
> --
Of course, you could fix that!
function T = true(N)
% Hacky-TRUE
T = ~zeros(N);
|
|
0
|
|
|
|
Reply
|
Matt
|
3/30/2010 10:10:22 PM
|
|
"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <hotso5$2ib$1@fred.mathworks.com>...
> Thanks for all the really helpful replies!
>
> I'm currently toying with the (perhaps hungry?) expression:
>
> ismember((1:A(end)), A)
>
> (I can assume A is in ascending order)
That works, but it will be slower and less readable than many of the other solutions.
|
|
0
|
|
|
|
Reply
|
Matt
|
3/30/2010 10:11:04 PM
|
|
dpb wrote:
> Nathan wrote:
> ...
>
>> If you were going to do that, why not just make it logical in the
>> first place?
>>
>> o(x) = true;
>
> Primarily because...
>
> >> which true
> true not found.
> >> help true
>
> true.m not found.
But if you have logical() at your disposal, then you could use
o(x) = logical(1);
So now who is going to give us an answer in terms of logical(accumarray(...))
? ;-)
|
|
0
|
|
|
|
Reply
|
Walter
|
3/30/2010 10:14:34 PM
|
|
Walter Roberson
> dpb wrote:
> > Nathan wrote:
> > ...
> >
> >> If you were going to do that, why not just make it logical in the
> >> first place?
> >>
> >> o(x) = true;
> >
> > Primarily because...
> >
> > >> which true
> > true not found.
> > >> help true
> >
> > true.m not found.
>
> But if you have logical() at your disposal, then you could use
>
> o(x) = logical(1);
>
>
> So now who is going to give us an answer in terms of logical(accumarray(...))
> ? ;-)
Out = accumarray(A.',true,[],@logical);
Another versione :). Only if unique(A) == A.
|
|
0
|
|
|
|
Reply
|
Oleg
|
3/30/2010 11:04:06 PM
|
|
Walter Roberson
> dpb wrote:
> > Nathan wrote:
> > ...
> >
> >> If you were going to do that, why not just make it logical in the
> >> first place?
> >>
> >> o(x) = true;
> >
> > Primarily because...
> >
> > >> which true
> > true not found.
> > >> help true
> >
> > true.m not found.
>
> But if you have logical() at your disposal, then you could use
>
> o(x) = logical(1);
>
>
> So now who is going to give us an answer in terms of logical(accumarray(...))
> ? ;-)
Out = accumarray(A.',true,[],@logical);
Another versione :). Only if unique(A) == A.
|
|
0
|
|
|
|
Reply
|
Oleg
|
3/30/2010 11:05:23 PM
|
|
Very nice, Oleg. Silly solutions are a lot more fun.
|
|
0
|
|
|
|
Reply
|
james
|
3/30/2010 11:40:26 PM
|
|
Oleg Komarov wrote:
....
>> But if you have logical() at your disposal, then you could use
>>
>> o(x) = logical(1);
>>
....
Indeedy, do...very good (and why didn't _I_ think of it??? :( )... :)
--
|
|
0
|
|
|
|
Reply
|
dpb
|
3/30/2010 11:41:08 PM
|
|
Matt Fig wrote:
> dpb <none@non.net> wrote in message
> <hotrs9$qbg$1@news.eternal-september.org>...
>> Nathan wrote:
>> ...
>>
>> > If you were going to do that, why not just make it logical in the
>> > first place?
>> > > o(x) = true;
>>
>> Primarily because...
>>
>> >> which true
>> true not found.
>> >> help true
>>
>> true.m not found.
>>
>> >>
>>
>> :(
>>
>> --
>
>
> Of course, you could fix that!
>
> function T = true(N)
> % Hacky-TRUE
> T = ~zeros(N);
true(aye-that);
--
|
|
0
|
|
|
|
Reply
|
dpb
|
3/30/2010 11:42:16 PM
|
|
Walter Roberson wrote:
> dpb wrote:
>> james bejon wrote:
>>> Dear All, Is there a neat way of getting from, say,
>>>
>>> [3, 5, 8]
>>>
>>> to
>>>
>>> [0, 0, 1, 0, 1, 0, 0, 1]?
>>
>> Alternately,
>>
>> o(x)=1; % create the 0/1 vector
>> l = logical(o); % turn it into a logical one
>>
>> Too bad for things like this ML doesn't support nested assignment..
>>
>> l = logical(o(x)=1); % Doesn't work, of course... :(
>
> Well, if you are going to be like that!
>
> l = any(bsxfun(@eq,1:max(x),x.'));
Chuckle...
--
|
|
0
|
|
|
|
Reply
|
dpb
|
3/30/2010 11:43:56 PM
|
|
dpb
> > l = any(bsxfun(@eq,1:max(x),x.'));
> Chuckle...
this is NOT funny - this is sheer, cruel abuse of ML syntax...
us
|
|
0
|
|
|
|
Reply
|
us
|
3/30/2010 11:54:06 PM
|
|
Oleg Komarov wrote:
>> So now who is going to give us an answer in terms of
>> logical(accumarray(...)) ? ;-)
>
> Out = accumarray(A.',true,[],@logical);
>
> Another versione :). Only if unique(A) == A.
Out = logical(accumarray(A.', 1));
This version does not require unique(A) == A.
I am not at all experienced in accumarray, but looking at the
documentation, it appears to me that your version would also work with
repeated elements if you changed the @logical to @any .
|
|
0
|
|
|
|
Reply
|
Walter
|
3/31/2010 3:09:41 AM
|
|
us wrote:
> dpb
>> > l = any(bsxfun(@eq,1:max(x),x.'));
>> Chuckle...
>
> this is NOT funny - ...
Not even a snicker??? :)
--
|
|
0
|
|
|
|
Reply
|
dpb
|
3/31/2010 4:45:38 AM
|
|
There's some really neat stuff here. Thanks so much. I'm very new to Matlab, but am starting to see how concise its code can be. On a related note, Can anyone think of a neat way of converting, say,
[3, 8, 12]
to
[1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]?
Or better still:
[3, 8, 12;
3, 7, 12]
to
[1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3;
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]?
|
|
0
|
|
|
|
Reply
|
james
|
3/31/2010 7:16:05 AM
|
|
Walter Roberson <roberson@hushmail.com> wrote in message <houedm$r10$1@canopus.cc.umanitoba.ca>...
> Oleg Komarov wrote:
>
> >> So now who is going to give us an answer in terms of
> >> logical(accumarray(...)) ? ;-)
> >
> > Out = accumarray(A.',true,[],@logical);
> >
> > Another versione :). Only if unique(A) == A.
>
> Out = logical(accumarray(A.', 1));
>
> This version does not require unique(A) == A.
>
> I am not at all experienced in accumarray, but looking at the
> documentation, it appears to me that your version would also work with
> repeated elements if you changed the @logical to @any .
You're right!
Oleg
|
|
0
|
|
|
|
Reply
|
Oleg
|
3/31/2010 7:23:06 AM
|
|
"james bejon"
> There's some really neat stuff here. Thanks so much. I'm very new to Matlab, but am starting to see how concise its code can be. On a related note, Can anyone think of a neat way of converting, say,
>
> [3, 8, 12]
>
> to
>
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]?
>
> Or better still:
>
> [3, 8, 12;
> 3, 7, 12]
>
> to
>
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3;
> 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]?
Be rude!
A = [3,8,12];
rude(A./(1:numel(A)), 1:numel(A)) % FEX by us
Oleg
|
|
0
|
|
|
|
Reply
|
Oleg
|
3/31/2010 7:33:04 AM
|
|
"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <housrl$1vl$1@fred.mathworks.com>...
> There's some really neat stuff here. Thanks so much. I'm very new to Matlab, but am starting to see how concise its code can be. On a related note, Can anyone think of a neat way of converting, say,
>
> [3, 8, 12]
>
> to
>
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]?
>
> Or better still:
>
> [3, 8, 12;
> 3, 7, 12]
>
> to
>
> [1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3;
> 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]?
c=[3, 8, 12;
3, 7, 12]
r = repmat((1:size(c,1)).',[1 size(c,2)]);
A = accumarray([r(:) c(:)+1],1);
A(:,1) = 1;
A = cumsum(A,2);
A(:,end) = [];
disp(A)
% Bruno
|
|
0
|
|
|
|
Reply
|
Bruno
|
3/31/2010 9:59:06 AM
|
|
Hi Bruno. Thanks very much for your suggestion. Works perfectly. I like your idea with the cumsum function. In the end, I've gone for something like (I happen to know that all the rows are of length 25)
c = transpose([3, 11, 23, 24; 3, 10, 23, 24]) + 1;
z = zeros(25, size(c, 2));
n = repmat(1:size(c, 2), size(c, 1), 1);
z(sub2ind(size(z), c, n)) = 1;
z = cumsum(z, 1) + 1;
P.S. I nicked the sub2ind idea from your following RANK thread!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/163003#731927
|
|
0
|
|
|
|
Reply
|
james
|
4/1/2010 12:15:22 AM
|
|
|
26 Replies
132 Views
(page loaded in 0.11 seconds)
Similiar Articles: Convert from std_logic_vector to real - comp.lang.vhdlIs there a standard library function (e.g., to_real) to convert a std_logic_vector to a real value? If not, how would one go about doing this? ... How to extract subarray ? - comp.lang.vhdlLogical Vectors From Values - comp.soft-sys.matlab How to extract subarray ? - comp.lang.vhdl Extract Matrix values using vector values ... ... or a length-one vector ... Problem dividing two vectors - comp.soft-sys.matlab... comp.soft-sys.matlab | Computer Group Convert from std_logic_vector to real - comp.lang.vhdl In this case, convert to integer, divide by 2**8 and assign to the real value. ... Test vector for only MSB being set. - comp.lang.vhdlOne of the operations it performs depends on the value being "1000.....0000 ... is better to write a function: function msb_only(n: positive) return std_logic_vector ... 8 bit binary to 2 digit BCD - comp.lang.vhdl... when GOT1 => > ValueKl:= Value mod 10; > -- TempValue:= Value - ValueKl; > -- ValueGr := TempValue * (10/100); > Register_1<= conv_std_logic_vector ... CONV_INTEGER ERROR - comp.lang.vhdlted.franklin3@gmail.com wrote: > Is there an issue with using the conv_integer function with the > value being a single bit slice (a 1 or a 0) of a std_logic_vector?? catching std::exception by value - comp.lang.c++.moderated ...String to std_logic_vector - comp.lang.vhdl... to_slv (c : CHARACTER) RETURN STD_LOGIC_VECTOR IS > variable Return_Value : STD ... std::exception::what() - comp.lang.c++ ... ModelSim & tcl testbench - comp.lang.vhdlmodelsim & tcl testbench - Forum for Electronics I'm trying to create testbench which creates sin(x) real value, then converts to std_logic_vector, then sends to VHDL ... Logic problem - comp.soft-sys.matlab... event is stored. one way to store it would be as a vector ... it is not quite clear to me what the correct logic you ... event_times=0.1*[1:13]; %length 13 array with values 0.1 ... help -- binary to LCD display - comp.lang.vhdlentity bintodec is PORT( clock:in std_logic; dip1: in std_logic_vector(7 downto 0 ... from what the others have mentioned, I don't see that var2 is assigned a value ... Assigning value to symbolic expressions in vector - comp.soft-sys ...Evaluating Logical Variables in SAS Macro - comp.soft-sys.sas ..... has any ... the circle's path, create a symbolic ... The result is either a three-value vector for ... perfcurve with multiclass classification problems - comp.soft-sys ...... array of strings, but it demands that the test labels should be a vector of values (so ... The labels can be a numeric vector, logical vector, character matrix, cell ... Simulation of VHDL code for a vending machine - comp.arch.fpga ...... that boolean more closely models reality in that real digital logic only has two values ... this whole mess with > integer/signed/unsigned/bit_vector/std_logic_vector ... Differences between multimap and map of vector - comp.lang.c++ ...If you know approximately how many values each key in map contains, you can ... write testbench file? - comp.lang.vhdl... downto 0); signal PWDATA_TOP: std_logic_vector ... How to write testbench file? - comp.lang.vhdl... PENABLE, PWRITE: in std_logic; DUTY_CYC: in std_logic_vector (2 ... Writing Hex values to file in VHDL? - comp.arch.fpga please help me for vhdl code ... Applications of logical vectors and matrices in MATLABApplications of logical vectors and matrices in MATLAB By Gilberto E. Urroz, August 2004 Logical statements in MATLAB are statements that return a value of true (1 ... R: Logical Operators - Penn State Department of StatisticsValue. For !, a logical or raw vector of the same length as x. For |, & and xor a logical or raw vector. The elements of shorter vectors are recycled as necessary (with a ... 7/14/2012 10:43:36 PM
|