Can I use boolean of a variable on a column of another variable

4 ビュー (過去 30 日間)
Femke R
Femke R 2020 年 10 月 6 日
コメント済み: Femke R 2020 年 10 月 7 日
Hi,
I'm new here to mathworks so I apologise if I don't give all the relevant information right away.
Say I have the variables target(range 1:4) and response(matrix with 10000x16 values)
I want to iterate through each response column and extract the values linked to a specific target and add them to a different vector (Like response(target==2) but for each column seperately)
Right now I have something like:
Resp=[]
For i=1:16
Resp=[Resp, response(:,i)(target==2)]
End
This doesn't work. How can i rewrite it?

採用された回答

Matt J
Matt J 2020 年 10 月 6 日
Resp=response(target==2,:);

その他の回答 (1 件)

Peter O
Peter O 2020 年 10 月 7 日
Hi, to confirm/rephrase, you're looking to extract 16 separate arrays, each one corresponding to the case where the response matches a target value? If so:
% Setup
target = 1:4;
response = randi(max(target),10000, 16); % Dummy data
% Get 16 different variables, one for each column.
% Because the variables are of potentially different lengths, you'll need to assign them to a cell array.
V = cell(16,1);
my_target = 2;
for ix=1:16
V{ix} = response(response(:,ix) == my_target, ix);
end
  1 件のコメント
Femke R
Femke R 2020 年 10 月 7 日
Yes! Exactly, thanks for the explanation (and a shorter way to add to a vector)

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by