# Use of for loop for multiple columns

58 views (last 30 days)
aa on 30 Aug 2020
Edited: VBBV on 5 Sep 2020
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 CommentsShowHide 3 older comments
Peter O on 30 Aug 2020
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)

Sign in to comment.

### Accepted Answer

VBBV on 5 Sep 2020
Edited: VBBV on 5 Sep 2020
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 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!