How can i store values calculated in my loop
10 ビュー (過去 30 日間)
古いコメントを表示
fatihveysel nurcin
2019 年 3 月 20 日
コメント済み: fatihveysel nurcin
2019 年 3 月 20 日
Here is my code, i managed to do the operation however i can't store each column, what i get is only result of latest column, In this code, first element of column is checked if its less than 10, if yes then element is replaced by zero, code will keep checking elements of column and replace them by zero until it reaches to number 10;
However; I can't store all the columns, only last column is stored, how can store all the columns that i can reconstruct the new matrix;
X1 is the new matrix
X = [9 20 15 9 10 5;5 20 5 20 15 3;15 29 39 49 5 10];
[rows columns depth]=size(X);
for r=1:columns; % search for each column
X1=X(:,r); %X1 is going to be new matrix
for k=1:3 %k will help me to go through each element in column
if X1(k)<10 % if first element is less than 10, replace it by 0 till
% i find the number which equal or higher than 10
X1(k)=0;
else
break % if number is already more than 10, don't search for other
% element in the column, switch to next column
end
end
end
0 件のコメント
採用された回答
Star Strider
2019 年 3 月 20 日
I am not certain what you are doing. One option is to create a second matrix (‘X2’ here), and store the results in it:
X = [9 20 15 9 10 5;5 20 5 20 15 3;15 29 39 49 5 10];
[rows columns depth]=size(X);
X2 = zeros(size(X));
for r=1:columns;
X1=X(:,r);
for k=1:3
if X1(k)<10
X1(k)=0;
else
break
end
end
X2(:,r) = X1;
end
producing:
X2 =
0 20 15 0 10 0
0 20 5 20 15 0
15 29 39 49 5 10
2 件のコメント
その他の回答 (1 件)
Andrei Bobrov
2019 年 3 月 20 日
編集済み: Andrei Bobrov
2019 年 3 月 20 日
X = [9 20 15 9 10 5;5 20 5 20 15 3;15 29 39 49 5 10];
[rows, columns]=size(X);
for r=1:columns
for k=1:3
if X(k,r)<10
X(k,r)=0;
else
break
end
end
end
or without loops:
X = X.*(cumsum(X >= 10) > 0);
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!