Zero Matrix Values using Indices

1 view (last 30 days)
Ben Whitby
Ben Whitby on 16 Sep 2022
Commented: Voss on 17 Sep 2022
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???????

Accepted Answer

Voss
Voss on 17 Sep 2022
Edited: Voss on 17 Sep 2022
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 Comments
Ben Whitby
Ben Whitby on 17 Sep 2022
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 on 17 Sep 2022
OK. I've updated my answer based on your updated question.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!