if you run F Zero you will get [2 5], I want to delete the column number 2 and 5 from the Matrix W, but the problem is when I'm deleting the columns it deletes number 2 and 6

1 Ansicht (letzte 30 Tage)
clc;
clear;
W=rand(6)
Pt=10;
Pn=[2 7 3 2 8 4];
P = waterfill(Pt,Pn);
F_Zero=find(~P) %Find Zeros
%% delete the columns
if (~isempty(F_Zero)) %There is Zeros (not empty)
for i=1:length(F_Zero) % Loop to Set Columns to Zeros
C_Zero=F_Zero(i);
W(:,C_Zero) = []
end
end

Akzeptierte Antwort

KSSV
KSSV am 8 Jun. 2021
When you are using a loop the ddimension of W changes and it removes the column in the newly obtained W. You need not to use loop. remove columns at once.
clc;
clear;
W=rand(6) ;
Pt=10;
Pn=[2 7 3 2 8 4];
P = waterfill(Pt,Pn);
F_Zero=find(~P) %Find Zeros
%% delete the columns
if (~isempty(F_Zero)) %There is Zeros (not empty)
W(:,F_Zero) = [] ;
end
  4 Kommentare
KSSV
KSSV am 8 Jun. 2021
I kept it for checking later whether columns deleted properly or not. You may remove this.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by