Filter löschen
Filter löschen

How to efficiently Assign Value to column vector?

27 Ansichten (letzte 30 Tage)
balandong
balandong am 23 Jul. 2017
Kommentiert: balandong am 23 Jul. 2017
Dear Coder, Any idea how to remove the two for loop to assign multiple value to a column vector? Really appreciate for the help.
The patients.mat is build-in mat file in MATLAB 2017
load patients
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[tf,Ioc_Alert_LessThree] = find (ismember(Smoker, 1));
for i = 1: length (tf)
M (i,1) = Height (tf(i));
end
[tgf,Ioc_Alert_two] = find (ismember(Smoker, 0));
for i = 1: length (Ioc_Alert_two)
M (i,2) = Height (tgf(i));
end
M(M == 0) = NaN;
NonSmoker(Ioc_Alert_two == 1) = 0;
Yes_Smoker(Ioc_Alert_LessThree == 1) = 1;
scatter ( M (1:34,1),Yes_Smoker)
hold on
scatter ( M (:,2),NonSmoker)
  2 Kommentare
Image Analyst
Image Analyst am 23 Jul. 2017
Bearbeitet: Image Analyst am 23 Jul. 2017
It would be easier for people to help you if you attached patients.mat. Then they could run your code.
balandong
balandong am 23 Jul. 2017
patients.mat is build-in mat file in MATLAB 2017

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 23 Jul. 2017
balandong - your first for loop is
for i = 1: length (tf)
M (i,1) = Height (tf(i));
end
Try replacing this with
M = zeros(length(tf),1);
M(1:length(tf)) = Height(tf(1:length(tf)))
Something similar can be done for the second for loop too.
  1 Kommentar
balandong
balandong am 23 Jul. 2017
Thanks Geoff, Your suggestion is more compact, indeed.
The full code is as following.
load patients
[tf,Ioc_Alert_LessThree] = find (ismember(Smoker, 1));
[tgf,Ioc_Alert_two] = find (ismember(Smoker, 0));
M = zeros(length(tgf),2);
M(1:length(tf),1) = Height(tf(1:length(tf)));
M(1:length(tgf),2) = Height(tgf(1:length(tgf)));

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by