Info

この質問は閉じられています。 編集または回答するには再度開いてください。

i want to store elements in matrix and call number wise but there call in workspace it seems undefined function and consider only last entry

1 回表示 (過去 30 日間)
Pratik Anandpara
Pratik Anandpara 2016 年 12 月 13 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
for d=1:18
parent=bitand(entry(d), 2.^(7:-1:0)) > 0;
end
(there is entry(18*1) matrix it has 18 decimal number)
when i apply this code than this all rows named as 'parent' but i want to call parent(11)it shown error how i call particular raw from this created parent matrix and only parent call than i shown only last entry as consider value
  2 件のコメント
Adam
Adam 2016 年 12 月 13 日
parent will have length equal to however many elements of your array 'entry' satisfy that condition, so at most 18, but could be 0 for an empty array too.

回答 (1 件)

dpb
dpb 2016 年 12 月 13 日
mask=8:-1:1;
parent=bsxfun(@bitget,entry,mask);
NB: Your logic for >1 is in error unless you don't want to report the LSB being set.
Also bitand and computing a power is much less efficient that just checking the bit position directly via bitget (altho must reverse order to reproduce your result).
Your code fails because you don't assign an indexing expression for the computed parent array the code will generate.
N=length(entry);
parent=zeros(N,8); % preallocate
for i=1:N
parent(i,:)=...
...
But, "the Matlab way" is to use vectorized solution as shown instead.

この質問は閉じられています。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by