P=[1 32 3 78 5 6 7 8 9 10] ;P is my original array which has the size is 1*10; I have Q=[2 4 7];I want delete in Q array value in P array; My expect output is P=[ 1 3 5 6 8 9 10] by using for loop only because of the size of Q array may be change ;P(:,[Q(1) Q(2) Q(3)])=[] this command use only the size of Q array is 1*3 .So I need for loop logic which is common of any array size.

 採用された回答

madhan ravi
madhan ravi 2018 年 11 月 8 日
編集済み: madhan ravi 2018 年 11 月 8 日

2 投票

P(Q)=[] %edited after Jan's comment

13 件のコメント

VIJAY
VIJAY 2018 年 11 月 8 日
Great ...
madhan ravi
madhan ravi 2018 年 11 月 8 日
Anytime :) @vijay
madhan ravi
madhan ravi 2018 年 11 月 8 日
編集済み: madhan ravi 2018 年 11 月 8 日
@stephen the OP want to delete the value of P where the Q value corresponds to the index of P , please before commenting read the question properly and comment
P=[1 32 3 78 5 6 7 8 9 10]
in the above P array can you find the value 2???
My expect output is P=[ 1 3 5 6 8 9 10]
2 in Q corresponds to index value 32 in P
4 in Q corresponds to index value 78 in P
7 in Q corresponds to index value 7 in P which needs to be deleted
Steven Lord
Steven Lord 2018 年 11 月 8 日
I interpreted the question the way madhan ravi did. I believe Q contains the indices of the elements in P that should be deleted, not the elements in P that should be deleted.
If VIJAY wanted the latter, I would use setdiff with the 'stable' flag to remove all instances of elements in Q from P, or find to locate just the first (or last) instance in P of each element in Q and delete those locations.
madhan ravi
madhan ravi 2018 年 11 月 8 日
The question is badly framed
madhan ravi
madhan ravi 2018 年 11 月 8 日
Thank you very much @Steven lord for justifying my answer
Stephan
Stephan 2018 年 11 月 8 日
I agree the question is unclear. Since it is accepted i will remove flag. Perhaps next time ask the OP to clarify....
madhan ravi
madhan ravi 2018 年 11 月 8 日
and remove your comments @stephen
Stephan
Stephan 2018 年 11 月 8 日
編集済み: Stephan 2018 年 11 月 8 日
I think we are clean now ;-)
+1
madhan ravi
madhan ravi 2018 年 11 月 8 日
Thank you @stephen :)
Stephan
Stephan 2018 年 11 月 8 日
No problem
Jan
Jan 2018 年 11 月 8 日
You can omit the square brackets and 1:end :
P(Q)=[]
madhan ravi
madhan ravi 2018 年 11 月 8 日
+1 @Jan cool

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

その他の回答 (1 件)

Stephan
Stephan 2018 年 11 月 8 日
編集済み: Stephan 2018 年 11 月 8 日

1 投票

If you want to delete the values at the indicies in P, that are specified in Q use the answer from Madhan above. If you need to delete the values in P specified in Q use:
P(ismember(P,Q))=[]

1 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 8 日
+1 another way of approach

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 11 月 8 日

編集済み:

2018 年 11 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by