Use of for loop for multiple columns

4 次查看(过去 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.
  4 个评论
aa
aa 2020-8-30
hi i am new .. try to apply but failed
Peter O
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
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 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by