필터 지우기
필터 지우기

Command for picking two regular elements from a Vector

조회 수: 1 (최근 30일)
Chaudhary P Patel
Chaudhary P Patel 2023년 7월 7일
편집: Matt J 2023년 7월 7일
I have a vector of size 30X 1
I want to pick up 1, 2, 4 , 5, 7, 8, 10, 11, ............28, 29.
please suggest me how can i proceed?
  댓글 수: 1
Chaudhary P Patel
Chaudhary P Patel 2023년 7월 7일
이동: Matt J 2023년 7월 7일
sir, i want all the values except multiple of 3 position.

댓글을 달려면 로그인하십시오.

답변 (2개)

Steven Lord
Steven Lord 2023년 7월 7일
Use randperm to create shuffled indices then use that index vector to reorder your vector of data. I'll use a deck of cards:
values = ["Ace", 2:10, "Jack", "Queen", "King"];
suits = ["club", "diamond", "heart", "spade"];
cards = combinations(values, suits);
order = randperm(height(cards)); % Random ordering of 1:52
shuffledDeck = cards(order, :); % Use order as indices into rows of cards table
Let's look at the first few cards of the two decks. First the one in order:
head(cards)
values suits ______ _________ "Ace" "club" "Ace" "diamond" "Ace" "heart" "Ace" "spade" "2" "club" "2" "diamond" "2" "heart" "2" "spade"
Next the shuffled deck.
head(shuffledDeck)
values suits ______ _________ "3" "club" "8" "diamond" "3" "heart" "Jack" "club" "10" "diamond" "7" "heart" "Ace" "club" "4" "club"
While I used table arrays here (cards, shuffledDeck), the same technique (i.e. shuffledDeck = cards(order, :); ) applies to regular arrays as well.

Satwik Samayamantry
Satwik Samayamantry 2023년 7월 7일
Hi Patel, as per my understanding you want to remove all multiples of 3 from your input vector. The following function should get your job done.
function outputVector = removeMultiplesOfThree(inputVector)
% Initialize an empty output vector
outputVector = [];
% Iterate through each element in the input vector
for i = 1:length(inputVector)
% Check if the current element is not a multiple of 3
if mod(inputVector(i), 3) ~= 0
% Append the element to the output vector
outputVector = [outputVector; inputVector(i)];
end
end
end
Hope this helps you!!
  댓글 수: 1
Matt J
Matt J 2023년 7월 7일
편집: Matt J 2023년 7월 7일
yourVector=randi(100,1,30) %Example
yourVector = 1×30
37 5 81 91 82 46 93 29 17 47 9 83 86 37 85 59 84 33 12 71 79 90 88 30 83 33 68 80 45 70
N=numel(yourVector);
subset=setdiff(1:N,3:3:N);
yourVector=yourVector(subset) %Result
yourVector = 1×20
37 5 91 82 93 29 47 9 86 37 59 84 12 71 90 88 83 33 80 45

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by