Zero Matrix Values using Indices

1 回表示 (過去 30 日間)
Ben Whitby
Ben Whitby 2022 年 9 月 16 日
コメント済み: Voss 2022 年 9 月 17 日
Hi All,
I have two matrices. The first is an 8x6 matrix (Speed_ENU) that contains some measured data. The second matrix (HubHeight) contains integers corresponding to particular rows in the first matrix. I want to set the values corresponding the indices in ind to zero. I have the follwing code so far. The comments in the code explain what I am trying to do.
This is as far as I get. How can I set the values in Speed_ENU corresponding to the Indexes in ind to zero???????
clc
clear all
Speed_ENU = [1,3,6,14,25,33; 11,12,15,45,15,67;1,32,16,4,25,3; 11,12,5,45,15,7;11,32,6,4,25,33; 1,12,5,44,15,67;11,31,6,4,25,3; 1,22,5,5,9,7 ]
Speed_ENU = 8×6
1 3 6 14 25 33 11 12 15 45 15 67 1 32 16 4 25 3 11 12 5 45 15 7 11 32 6 4 25 33 1 12 5 44 15 67 11 31 6 4 25 3 1 22 5 5 9 7
HubHeight = [6,6,6,4,5,5]
HubHeight = 1×6
6 6 6 4 5 5
[sz1,sz2]=size(Speed_ENU);
row = HubHeight; %We want to set all the values at Hub Height to zero
col = [1:sz2];
sz = size(Speed_ENU)
sz = 1×2
8 6
ind = sub2ind(sz,row,col) %Returns the linear Indices corresponding to the row and column subscripts in Speed_ENU
ind = 1×6
6 14 22 28 37 45
Speed = (Speed_ENU(ind)); %This is as far as I get. How can I set the values in Speed_ENU corresponding to
% the Indexes in ind to zero???????

採用された回答

Voss
Voss 2022 年 9 月 17 日
編集済み: Voss 2022 年 9 月 17 日
Speed_ENU = [1,3,6,14,25,33; 11,12,15,45,15,67;1,32,16,4,25,3; 11,12,5,45,15,7;11,32,6,4,25,33; 1,12,5,44,15,67;11,31,6,4,25,3; 1,22,5,5,9,7]
Speed_ENU = 8×6
1 3 6 14 25 33 11 12 15 45 15 67 1 32 16 4 25 3 11 12 5 45 15 7 11 32 6 4 25 33 1 12 5 44 15 67 11 31 6 4 25 3 1 22 5 5 9 7
HubHeight = [6,6,6,4,5,5];
[sz1,sz2] = size(Speed_ENU);
% row = HubHeight;
% col = 1:sz2;
% sz = size(Speed_ENU);
% ind = sub2ind(sz,row,col);
% no need for the variables row, col, sz:
% (no need for ind either, really)
ind = sub2ind([sz1 sz2],HubHeight,1:sz2)
ind = 1×6
6 14 22 28 37 45
Speed_ENU(ind) = 0
Speed_ENU = 8×6
1 3 6 14 25 33 11 12 15 45 15 67 1 32 16 4 25 3 11 12 5 0 15 7 11 32 6 4 0 0 0 0 0 44 15 67 11 31 6 4 25 3 1 22 5 5 9 7
  2 件のコメント
Ben Whitby
Ben Whitby 2022 年 9 月 17 日
No. I need to set the indidual cells corresponding to the indices in ind ( 6 14 22 28 37 45) to zero. Not the entire row. The final matrix should look like this:
1 3 6 14 25 33
11 12 15 45 15 67
1 32 16 4 25 3
11 12 5 0 15 7
1 32 6 4 0 0
0 0 0 44 15 67
11 31 6 4 25 3
1 22 5 5 9 7
Voss
Voss 2022 年 9 月 17 日
OK. I've updated my answer based on your updated question.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by