How to create dynamic array with flexible index?

I am working with an RGB image and I am new to matlab.I have generated a probability matrix 1*40. Now I want to create a dynamic array with flexible index because I don't know how many values will satisfy the condition(I want to change 'P' to 1*80). for example P={0.4,0.5,0.7,0.2,0.1,0.9,0.5,0.4..........(40 values)}.If value ==0.5 I want to store the index value in an array. A(1)={2},A(2)={7}.....
Thanks in advance.

 採用された回答

KSSV
KSSV 2017 年 5 月 5 日

1 投票

A = rand(1,40) ;
%%values of A less then 0.5
P = A(A<0.5) ;
%%indices of A less then 0.5
idx = find(A<0.5) ;
You need not to worry about the dimension of array which you want obeying certain condition.

6 件のコメント

Ad
Ad 2017 年 5 月 5 日
Thank you for your response. but I am not getting the desired result. Getting this error
"Index exceeds matrix dimensions".
1.Why did you take rand() function? I need to check all index values.
2.A(A<0.5) will return the values or indices?
Stephen23
Stephen23 2017 年 5 月 5 日
編集済み: Stephen23 2017 年 5 月 5 日
  1. Because you did not supply us with any example data, KSSV created some sample data using rand. When we write code we also test that code to make sure that it works. If you do not give us sample data, what do you expect us to test our code on?
  2. The values. The logical comparison A<0.5 will return the indices.
Ad
Ad 2017 年 5 月 5 日
編集済み: Ad 2017 年 5 月 5 日
@KSSV,STEPHEN COBELDICK,Sorry for the trouble.
P={0.4,0.5,0.7,0.2,0.1,0.9,0.5,0.4,0.5,0.5}.If P(i) ==0.5. then Array A should store the index.
Expected result A{}={2,7,9,10}%Index values.
Second case:P={0.5,0.6,0.5,0.3}; Expected output A={1,3}
We didn't fix the Array A size because The P values may increase if I increase the Image size.
Adam
Adam 2017 年 5 月 5 日
Why are you working with cell arrays? Use numeric arrays. Or if you are using numeric arrays please use correct syntax when posting bits of code rather than just using { } to represent a grouping of numbers.
For what you want (assuming you made a typo and your condition in the example is == rather than <= and also that you use numeric arrays) you just need part of KSSV's answer:
P=[0.4,0.5,0.7,0.2,0.1,0.9,0.5,0.4,0.5,0.5];
A = find( P == 0.5 );
Ad
Ad 2017 年 5 月 5 日
編集済み: Ad 2017 年 5 月 5 日
@KSSV,STEPHEN COBELDICK and ADAM.
Thank you so much guys for your kind support.
Walter Roberson
Walter Roberson 2017 年 5 月 7 日
Out of all of those values, 0.5 is the only one that you can hope to be able to find by using equality testing.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

Ad
2017 年 5 月 5 日

コメント済み:

2017 年 5 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by