Removing the columns of a matrix

1 次查看(过去 30 天)
Hi,
I try to write a code that compares strings in a cell array. In this code, after it compares the strings in the cell array "type", it assigns the corresponding element in lat matrix to lat1 matrix. This situation is same in lat2 and long2 matrices. But in if statement, when it TF≠1, then it assigns 0 element in lat1 matrix. How can I remove the 0 elements from lat1,lat2,long1 and long2 matrices?
clear all
[lat,long,station,type]=textread('latandlong.txt','%f%f%s%s');
lat1=[];
long1=[];
lat2=[];
long2=[];
for i=1:24
TF=strcmpi('down',type{i,:})
if TF==1
lat1(i,:)=lat(i,:);
long1(i,:)=long(i,:);
elseif TF==0
lat2(i,:)=lat(i,:);
long2(i,:)=long(i,:);
end
end
  1 个评论
Jan
Jan 2011-10-19
About the useless "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301

请先登录,再进行评论。

采纳的回答

Jan
Jan 2011-10-19
The 0 are not written into lat1 in the "if TF==0" block, but lat1(1, :) is filled with zeros automatically if you assign lat1(2, :). Try this:
x = [];
x(2) = 5
Modification of your code:
lat1=[];
long1=[];
lat2=[];
long2=[];
for i=1:24
TF=strcmpi('down', typeC{i,:}) % "type" => "typeC"!
if TF==1
lat1 = cat(1, lat1, lat(i,:));
long1 = cat(1, long1, long(i,:));
elseif TF==0
lat2 = cat(1, lat2, lat(i,:));
long2 = cat(1, long2, long(i,:));
end
end
But letting a matrix grow in each iteration is very inefficient. Faster and nicer:
index = strcmpi('down', typeC); % "type" => "typeC"!
lat1 = lat(index, :);
long1 = long(index, :);
lat2 = lat(~index, :);
long2 = long(~index, :);
Do not use "type" as name of a variable, because this shadows the built-in function with the same name.

更多回答(2 个)

Okan
Okan 2011-10-19
Thank you :) This completely removes the problem.

Okan
Okan 2011-10-19
In addition, I have to draw a circle with r=0.70 around these station point. Also, its center should be at x=139.7656, y=36.38695 How can I draw this circle?

Community Treasure Hunt

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

Start Hunting!

Translated by