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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by