![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/353672/image.jpeg)
Use of for loop for multiple columns
11 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone,
May someone help me.
The code presneted here is work well. I need to modified this for n number of columns (a=s(:,1) is the first column of S). s file consists of 1000 columns.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/353669/image.jpeg)
4 件のコメント
Peter O
2020 年 8 月 30 日
Rafael's suggestions put you on the right track, and he's given you the generalized code for k columns. If I can suggest a slight correction to the array access so that all of the columns are extracted an operated on:
clear all
clc
n_cols = 1000;
s = importdata('data.xlsx');
a=s(:,n_cols);
s_ev = sum(a(1:24,:)); % A slight correction here to extract all columns to pass to the sum function.
% This applies the zero to 0.25 substitution across columns
s_ev(s_ev == 0) = 0.25;
r = s_ev/24;
t = a(25:48,:);
% Taking a guess here that your intent is to divide the next 24 rows by the average for the first 24 rows, for every column.
% This takes advantage of some of MATLAB's broadcast operations to eliminate the for loop.
b = t - r./sqrt(r);
% Gives you the max for each column
c = max(b)
採用された回答
VBBV
2020 年 9 月 5 日
編集済み: VBBV
2020 年 9 月 5 日
Try this way
% If true
% code
% end
clear all
clc
s = importdata('data.xlsx');
[R,C] = size(s)
for j = 1:C
a=s(:,j);
end
s_ev = sum(a(1:24,:));
% A slight correction here to extract all columns to pass to the sum function.
%This applies the zero to 0.25 substitution across columns
s_ev(s_ev == 0) = 0.25;
r = s_ev/24;
t = a(25:48,:);
% Taking a guess here that your intent is to divide the next 24 rows by the average for the first 24 rows, for every column.
% This takes advantage of some of MATLAB's broadcast operations to eliminate the for loop.
b = t - r./sqrt(r);
%Gives you the max for each column
c = max(b)
Make sure that you have only numeric data in .xls file of importdata function
0 件のコメント
その他の回答 (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!