Make a vector into a call of a function
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi, i would like to use bi2de for some elements of a vector A. If A(1)=0 A(2)=0 A(3)=1 and i call bi2de as bi2de(A(1:3)) i get the dec of each element A(1) A(2) and A(3). I would like to take the dec of bi2de([A(1) A(2) A(3)]). How can i create this vector for random number of elements in [ ] without creating the vector before for example using while-loop. thanks
採用された回答
According to your definition
A(1)=0 A(2)=0 A(3)=1
then we get
A = [0,0,1]
which means we can simply call
bi2de(A)
Whatever method you use it will have to create a vector (or matrix) before these values are passed to bi2de.
7 件のコメント
Nikos Karamolegkos
2015 年 4 月 13 日
sure, but A has length larger than 3. If A has size 1x12 i want to take first 3 values or 2 or 5, not all the vector, so i have to create a sub-vector every time with this way [A(1) A(2) ... A(something)]. I want a not hard coded method.
Then you can use basic MATLAB indexing, e.g. to use the first three elements of A:
N = 3;
bi2dec(A(1:N))
Or do you imagine something else? If so, please explain with examples of what you would like to have as input and outputs.
Nikos Karamolegkos
2015 年 4 月 13 日
thanks for your responce. First i am using bi2de , second if i try bi2de(A(1:something)) and A has the values A(1)=0 A(2)=0 and A(3)=1 then i have as result a vector with 3 elements. The first element is 0 the second is 0 and the third is 1. That means that bi2de is running for each element of A(1:3) (think it as 3-loops) i want this to run one time such as call her for vector [A(1) A(2) A(3)]=[0 0 1]=4 (bi2de has msb the last (right element)) doing this one value is returned. Is it now clear?
No, it's really not clear. Note that
A(1:3)
is exactly the same as
[A(1) A(2) A(3)]
assuming A is a row vector.
If A is a column vector or matrix then,
A(1:3).'
is exactly the same
Nikos Karamolegkos
2015 年 4 月 13 日
編集済み: Nikos Karamolegkos
2015 年 4 月 13 日
of course let me try to be more specific. I have a function bi2de (check help) i have a vector A size lets tell 12. if i write: -bi2de(A(1)) i will take 0 -bi2de(A(2)) i will take 0 -bi2de(A(3)) i will take 1
that is exactly the same as writing bi2de(A(1:3)) i will take a new vector that will be 0, 0, 1
I don't want this. I want to be give A(1) A(2) A(3) as one bin value the value 001. If i make it hard coded as temp=[A(1) A(2) A(3)] and write bi2de(temp) the function will return one time (one value) that it will be 4 (i explained the reason before). I want to create the vector temp not hard coded but automatically for random-but-known end_index value. for example temp=[A(1) A(2) A(3) ... A(end_index)] that can be 000100 and call bi2de for 00010 and take one returned value. Try to call bi2de it will help.
"-bi2de(A(1)) i will take 0 -bi2de(A(2)) i will take 0 -bi2de(A(3)) i will take 1, that is exactly the same as writing bi2de(A(1:3))"
Except that these are not the same same thing, if A is a row vector. Try it. Your confusion might be happening because you have a column vector for A: is A a column vector? What do you get if you call this?:
size(A)
The vector-orientation is very important, as will be clear when you read the documentation and try some examples:
>> bi2de([0,0,1])
ans =
4
>> bi2de([0;0;1])
ans =
0
0
1
"I want to be give A(1) A(2) A(3) as one bin value the value 001"
Which is exactly how bi2de treats a horizontal vector input, in which case you can use any of my suggested solutions. If your input vector A is a column vector, then the function bi2de will treat each row as a separate value to convert, as the documentation clearly states. You can simply transpose the input vector to turn it into a horizontal vector if you want it considered as one value:
>> A = randi([0,1],12,1)
A =
0
0
1
1
0
0
0
1
1
1
0
1
>> bi2de(A(1:3)) % each row as separate value
ans =
0
0
1
>> bi2de(A(1:3).') % transposed into row vector
ans =
4
Nikos Karamolegkos
2015 年 4 月 14 日
it is working thanks. i forgot basic things
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
