MATLAB: How to determine whether any elements of an array are a certain a number - matlab

I am creating a simple minesweeper game for an assignment, but I am having trouble making the program determine whether the player has won or not. I am trying to find a function that determines whether any of the elements of A are equal to -2 and then use that in a while loop.
I have a simplified test code that I have been using to try to do this, but I can't find any functions that do what I want to do.
With everything I've tried so far, it either just keeps asking for more input even when it should say that the game is over, or it skips the while loop entirely. The last thing probably happens because all the elements of the start array are equal.
I have looked at xor, unique, setdiff, intersect, ismember isempty, and probably a couple more that I can't remember.
As this is not the complete code, I have just answered yes to the flag question every time in order to change all the elements from -2 to -3.
I also know that this wouldn't be the best way to determine whether the player has won or not, as it doesn't take into consideration if all the flags are placed correctly, but I wanna sort this part out first before I get on to that:)
A=zeros(2)
selected=0;
flag=-3
for r=1:2
for c=1:2
A(r,c)==-2;
end
end
while % any of the elements in A are equal to -2
while selected~=-1
selectRow=input('Which row is the cell you would like to access on? ');
selectCol=input('Which column is the cell you would like to access on? ');
selectFlag=input('Would you like to put a flag in this cell? ','s');
if selectRow<=2 && selectRow>=1 && selectCol<=2 && selectCol>=1
while strcmp(selectFlag, 'yes') || strcmp(selectFlag, 'no')
if strcmp(selectFlag, 'yes')
A(selectRow,selectCol)=flag;
disp(A);
elseif strcmp(selectFlag, 'no')
selected=mineBoard(selectRow,selectCol);
A(selectRow,selectCol)=selected;
disp(A);
end
end
end
end
fprintf('You have hit a mine. Please restart.\n');
end
fprintf('Congrats! You have won!');

There are multiple ways to know if an entry of A is equal to -2 and how to interact with the location of all those entries.
The any function
As suggested by #etmuse and #Wolfie in the comments, the any function may be best here. Notice in the documentation that more advanced options are available for versions R2018b and later.
% MATLAB R2017a
rng(8675309) % For reproducibility (ensure some -2 appear)
A = randi([-4 0],4) % Generate example matrix
any(A == -2,1) % Test columns of A
any(A == -2,2) % Test rows of A
Logical Index
% Use a logical index
idx = (A == -2)
Indices
It is a bit trickier to use the indices.
% Use the indices directly.
ind = find(A==-2)
Notice this gives you the indices if the array A were converted to vector form.
A(:) % Force column vector
reshape(A(:),4,4)' % Reshape into matrix
Disclosure: If #etmuse or #Wolfie post answer with any function, will remove that from my answer or make this community wiki. It was their idea.

Related

I want to remove all the points that satisfy certain condition in matlab

current plot
I want to remove all those data points that lies between 5 & 10 on x axis and 0 & 500 on y axis.
givenData is a 10000X2 matrix with data.
I've written the following code. What mistake am I making here? Also is there a better way to do this?
for i=1:10000
if givenData(i,1)>5 && givenData(i,1)<10 && givenData(i,2)>0 && givenData(i,2)<500
givenData(i,:) = [];
end
end
plot(givenData(:,1),givenData(:,2),'b.','MarkerSize',5);hold
contour(xgrid,ygrid,Z,[4e-5, 4e-5],'EdgeColor',[1 0 0],'ShowText','on','LineWidth',2);
Any help is appreciated. Thank you.
You’re deleting elements from the array as you iterate through it, so the next element shifts over into the deleted spot and gets skipped in the checking.
To fix this, you can either skip the loop entirely and use logical indexing:
givenData(givenData(:,1)>5 & givenData(:,1)<10 & givenData(:,2)>0 & givenData(:,2)<500,:) = [];
or iterate through the array backwards:
for i = 10000:-1:1
In general, avoiding the loop allows MATLAB to perform operations more quickly, with the trade off that the index arrays take more memory to process.

Matlab roll one dice game by using python and for sentence to add number

The question is this. As a result of consideration, I think if this condition is satisfied the code will be success.
Set n = 0, generate one of the integers 1~6 uniformly, Add a win if 1 was generated and add a lose otherwise, n++, 4. go back to 1 if n
but I don't know how to make it.can you please help me
N=100;
win=0;
lose=0;
a=randi([1 6],1,1);
n=0;
p=0;
while n<N
if a==1
win=win+1;
else
lose=lose+1;
n++
endif
endwhile
There are a couple of errors in your code:
you are generating only one random number, since the call to randi is outside of the while loop. So you are testing a==1 N times with a having always the same value
a part from the previous error, you are incrementing the counter n only in the else condition
A possible implementation could be the following in which you can incluide the code in a for loop to check the percentage of win wrt the number of attempts; you can also add the reference value of 1/6 %.
% Define the winning number
win_value=1
for N=1:1000
% Generate N random values
result=accumarray(randi([1 6],N,1),1);
% Count the wins
win(N)=result(win_value);
Pct_win(N)=win(N)/N*100;
end
plot(Pct_win)
hold on
plot([1 N],[1/6 1/6]*100,'r','linewidth',2)
xlabel('Attempts')
ylabel('Win %')
legend('Wins','Ref')

Extract parts of a big matrix and allocate them in new variables with loop function

I am a total beginner in MATLAB and I hope to find some help here. I have some model prediction results for 80 individuals alltogether in one large matrix. I need to extract the data for each individual from the big matrix, assign them in a new variable/matrix, do some extra calculations and then plot certain information as needed.
To do so, I am trying to write a script with a loop function but in a complicated, or maybe more accurately: in a primitive way!
Simplified Example:
My matrix is called: All_Indi_Data .... its dimension is: 600 rows x 21 columns
%Column 1: grouping variable (e.g., code or ID with values 1,2,3,4,5, etc.);
%Column 2: independent var.;
%Column 3: t;
%Column 4: OBS;
%Column 5: PRED;
i= length (All_Indi_Data);
%% First Indi.
q=1; % indicating the ID of the indi for which I want to extract the data
j=1; % variable added to insure writing start from the first row
for r=1:i
if All_Indi_Data (r,1)==q
Indi_1 (j,1:21) = All_Indi_Data (r,1:21)
j=j+1
end
end
%% Second Indi.
q=q+1
j=1
for r=1:i
if All_Indi_Data (r,1)==q
Indi_2 (j,1:21) = All_Indi_Data (r,1:21)
j=j+1
end
end
.
.
.
1) My first question is: can I allocate these data in new variables (Indi_1, Indi_2, ect.) in a more simple way with or without the loop function?!!! I would appreciate your help a lot.
2) Is there any code or any way to plot these selected parts (according to the grouping variable, e.g. data for Indi_1) from the previously mentioned big matrix without wasting a lot of time and space (wto recopying the core part of the code again and again) for the script, and using the loop function?! in other words, I would like to detect - with loop function & the grouping variable- which values are of interest and then to plot them (e.g. data in colum 3 with data from column 4 for each individual, starting from the first to the last)?!
I hope that I described my problem clearly and hope to hear something from the expert guys :) ...
Thanks a lot in advance ..
Try the following code:
for idx=1:80
pos=find(All_Indi_Data(:,1)==idx);
eval(['Indi_' num2str(idx) '=All_Indi_Data(pos,:);']);
end
What I do is: in each iteration, I search for a value of the ID, indicated in the variable idx. Note that I do not use ´i´ as the name of a variable, because Matlab uses it and ´j´ and the imaginary unit for complex numbers and that could cause problems.
Then, using find I search for the position (or positions) of All_Indi_Data in which I can find the information of that individual. Now I have in the variable ´pos´ the indexes of the rows in which there is information for the individual of interest.
Finally, using eval I extract the data for each individual into a variable. Note that eval combined with a loop makes it easy to create lots of variables. I indicate the rows I want to extract with ´pos´ and, as I want all the columns, I use just ´:´ (you could use ´1:21´ too).
With another similar loop you can plot the information you want. For example:
for idx=1:80
eval(['x=Indi_' num2str(idx) ';']);
% Now I have in X the information for this individual
%Plot the columns of x I want
plot(x(:, 3), x(:,4));
pause; %stay here until a press a key
end

How to solve error 'Improper assignment with rectangular empty matrix'?

I have written this messy code. When I run the first part of the code using the function daa, some times it produces a result, but sometimes it produces an error message. Does anyone have some ideas how to correct it?
Improper assignment with rectangular empty matrix.
Error in daa (line 26)
favoritec(i)=find(sPref(i,:)==bestmatch(i));
It's especially a problem, when I increased the dimension of n and m
This is the code
clear all;
n=4;
m=2;
Q=[1;1];
sPref=zeros(n,m);
cPref=zeros(m,n);
for i=1:n
sPref(i,:)=randperm(m);
end
for j=1:m
cPref(j,:)=randperm(n);
end
match=daa(sPref,cPref,Q)
Then the function daa is defined as following:
function match=daa(sPref,cPref,Q)
proposition=false(size(sPref)); % keep track who has proposed to which. False= not proposed yet
match=zeros(length(sPref),1);
favoritec=zeros(length(sPref),1);
numberS=zeros(size(cPref,1),1);
bestmatch=zeros(length(sPref),1);
iter=0;
while (min(match)==0) % as long as there is one unmatched, continues the loop (except the break)
iter=iter+1;
app=find(match==0);
for i=app(1:end)'
notProposed=(proposition(i,:)==false);
bestmatch(i)=min(sPref(i,notProposed));
favoritec(i)=find(sPref(i,:)==bestmatch(i));
numberS(favoritec(i))= numberS(favoritec(i))+1; % keep track of the no.of applicants
proposition(i,bestmatch(i))=1; % propsed to college j finishes,either reject or accept
end
% college deciding...
for j=1:size(cPref,1)
S_comp=find(favoritec==j); % find the students competing for the same Favoritec
if numberS(j) <=Q(j) % sum of students at the college smaller or equal than quota
match(S_comp)=favoritec(S_comp); % accept tentative offer
numberS(j)=sum(match==j);
sPref(S_comp,j)=NaN;
else
noapl=setxor(1:length(cPref),S_comp);
cPreft=cPref(j,:); % truncated pref,change the pref of those who didn't apply to NaN
cPreft(noapl)=NaN;
[r,I]=sort(cPreft);
topq=I(1:Q(j)); % college takes the top quota q students
match(S_comp)=0; % clean the previous assignment
match(topq)= favoritec(topq);
numberS(favoritec)=Q(j);
rejapp=setxor(S_comp,topq); % the students who got rejected
sPref(rejapp,j)=NaN;
end
%display(match);
end
%% if all choices have proposed, then stop
if proposition(i,:)==true;
display('already proposed to every college')
display(i)
break
end
end
Background
I believe that 'Improper assignment with rectangular empty matrix' means that you tried to assign a rectangular empty matrix to a scalar location. A rectangular empty matrix is a matrix that displays as "Empty matrix: 0-by-1". One way to generate such a matrix is to do a find on a matrix that is all false or that is a rectangular empty matrix itself.
Answer
In your code, this error occurs because there were no instances where sPref(i,:)==bestmatch(i).
If you type
rng(1237)
And then execute your code (without the clear all). You can reproduce the error.
Looking at the variables just before daa is called, you can see that daa([2, 1; 2, 1; 1, 2; 2, 1], [2, 1, 3, 4; 4, 2, 1, 3], [1;1]) fails. Another breakpoint reveals that sPref(1,:) = [2,NaN] and that notProposed=[false, true] so bestmatch(1) is NaN - which is never equal to anything. This indicates that the bug likely lies in how you assign NaNs to sPref in the next section.
You'll need to find that bug yourself. But this should answer your question about the 'improper assignment' error.
Unsolicited advice
This question should be tagged "matlab". Tag your post with the principal tool or language with which you are having problems so that the right people read it.
A short set of steps to reproduce makes it much easier to answer your question. It would have been better to try a few seed values to rng and include the daa function call than the large set of steps with random numbers.

What is your favourite MATLAB/Octave programming trick? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I think everyone would agree that the MATLAB language is not pretty, or particularly consistent. But nevermind! We still have to use it to get things done.
What are your favourite tricks for making things easier? Let's have one per answer so people can vote them up if they agree. Also, try to illustrate your answer with an example.
Using the built-in profiler to see where the hot parts of my code are:
profile on
% some lines of code
profile off
profile viewer
or just using the built in tic and toc to get quick timings:
tic;
% some lines of code
toc;
Directly extracting the elements of a matrix that satisfy a particular condition, using logical arrays:
x = rand(1,50) .* 100;
xpart = x( x > 20 & x < 35);
Now xpart contains only those elements of x which lie in the specified range.
Provide quick access to other function documentation by adding a "SEE ALSO" line to the help comments. First, you must include the name of the function in all caps as the first comment line. Do your usual comment header stuff, then put SEE ALSO with a comma separated list of other related functions.
function y = transmog(x)
%TRANSMOG Transmogrifies a matrix X using reverse orthogonal eigenvectors
%
% Usage:
% y = transmog(x)
%
% SEE ALSO
% UNTRANSMOG, TRANSMOG2
When you type "help transmog" at the command line, you will see all the comments in this comment header, with hyperlinks to the comment headers for the other functions listed.
Turn a matrix into a vector using a single colon.
x = rand(4,4);
x(:)
Vectorizing loops. There are lots of ways to do this, and it is entertaining to look for loops in your code and see how they can be vectorized. The performance is astonishingly faster with vector operations!
Anonymous functions, for a few reasons:
to make a quick function for one-off uses, like 3x^2+2x+7. (see listing below) This is useful for functions like quad and fminbnd that take functions as arguments. It's also convenient in scripts (.m files that don't start with a function header) since unlike true functions you can't include subfunctions.
for closures -- although anonymous functions are a little limiting as there doesn't seem to be a way to have assignment within them to mutate state.
.
% quick functions
f = #(x) 3*x.^2 + 2*x + 7;
t = (0:0.001:1);
plot(t,f(t),t,f(2*t),t,f(3*t));
% closures (linfunc below is a function that returns a function,
% and the outer functions arguments are held for the lifetime
% of the returned function.
linfunc = #(m,b) #(x) m*x+b;
C2F = linfunc(9/5, 32);
F2C = linfunc(5/9, -32*5/9);
Matlab's bsxfun, arrayfun, cellfun, and structfun are quite interesting and often save a loop.
M = rand(1000, 1000);
v = rand(1000, 1);
c = bsxfun(#plus, M, v);
This code, for instance, adds column-vector v to each column of matrix M.
Though, in performance critical parts of your application you should benchmark these functions versus the trivial for-loop because often loops are still faster.
LaTeX mode for formulas in graphs: In one of the recent releases (R2006?) you add the additional arguments ,'Interpreter','latex' at the end of a function call and it will use LaTeX rendering. Here's an example:
t=(0:0.001:1);
plot(t,sin(2*pi*[t ; t+0.25]));
xlabel('t');
ylabel('$\hat{y}_k=sin 2\pi (t+{k \over 4})$','Interpreter','latex');
legend({'$\hat{y}_0$','$\hat{y}_1$'},'Interpreter','latex');
Not sure when they added it, but it works with R2006b in the text(), title(), xlabel(), ylabel(), zlabel(), and even legend() functions. Just make sure the syntax you are using is not ambiguous (so with legend() you need to specify the strings as a cell array).
Using xlim and ylim to draw vertical and horizontal lines. Examples:
Draw a horizontal line at y=10:
line(xlim, [10 10])
Draw vertical line at x=5:
line([5 5], ylim)
Here's a quick example:
I find the comma separated list syntax quite useful for building function calls:
% Build a list of args, like so:
args = {'a', 1, 'b', 2};
% Then expand this into arguments:
output = func(args{:})
Here's a bunch of nonobvious functions that are useful from time to time:
mfilename (returns the name of the currently running MATLAB script)
dbstack (gives you access to the names & line numbers of the matlab function stack)
keyboard (stops execution and yields control to the debugging prompt; this is why there's a K in the debug prompt K>>
dbstop error (automatically puts you in debug mode stopped at the line that triggers an error)
I like using function handles for lots of reasons. For one, they are the closest thing I've found in MATLAB to pointers, so you can create reference-like behavior for objects. There are a few neat (and simpler) things you can do with them, too. For example, replacing a switch statement:
switch number,
case 1,
outargs = fcn1(inargs);
case 2,
outargs = fcn2(inargs);
...
end
%
%can be turned into
%
fcnArray = {#fcn1, #fcn2, ...};
outargs = fcnArray{number}(inargs);
I just think little things like that are cool.
Using nargin to set default values for optional arguments and using nargout to set optional output arguments. Quick example
function hLine=myplot(x,y,plotColor,markerType)
% set defaults for optional paramters
if nargin<4, markerType='none'; end
if nargin<3, plotColor='k'; end
hL = plot(x,y,'linetype','-', ...
'color',plotColor, ...
'marker',markerType, ...
'markerFaceColor',plotColor,'markerEdgeColor',plotColor);
% return handle of plot object if required
if nargout>0, hLine = hL; end
Invoking Java code from Matlab
cellfun and arrayfun for automated for loops.
Oh, and reverse an array
v = 1:10;
v_reverse = v(length(v):-1:1);
conditional arguments in the left-hand side of an assignment:
t = (0:0.005:10)';
x = sin(2*pi*t);
x(x>0.5 & t<5) = 0.5;
% This limits all values of x to a maximum of 0.5, where t<5
plot(t,x);
Know your axis properties! There are all sorts of things you can set to tweak the default plotting properties to do what you want:
set(gca,'fontsize',8,'linestyleorder','-','linewidth',0.3,'xtick',1:2:9);
(as an example, sets the fontsize to 8pt, linestyles of all new lines to all be solid and their width 0.3pt, and the xtick points to be [1 3 5 7 9])
Line and figure properties are also useful, but I find myself using axis properties the most.
Be strict with specifying dimensions when using aggregation functions like min, max, mean, diff, sum, any, all,...
For instance the line:
reldiff = diff(a) ./ a(1:end-1)
might work well to compute relative differences of elements in a vector, however in case the vector degenerates to just one element the computation fails:
>> a=rand(1,7);
>> diff(a) ./ a(1:end-1)
ans =
-0.5822 -0.9935 224.2015 0.2708 -0.3328 0.0458
>> a=1;
>> diff(a) ./ a(1:end-1)
??? Error using ==> rdivide
Matrix dimensions must agree.
If you specify the correct dimensions to your functions, this line returns an empty 1-by-0 matrix, which is correct:
>> diff(a, [], 2) ./ a(1, 1:end-1)
ans =
Empty matrix: 1-by-0
>>
The same goes for a min-function which usually computes minimums over columns on a matrix, until the matrix only consists of one row. - Then it will return the minimum over the row unless the dimension parameter states otherwise, and probably break your application.
I can almost guarantee you that consequently setting the dimensions of these aggregation functions will save you quite some debugging work later on.
At least that would have been the case for me. :)
The colon operator for the manipulation of arrays.
#ScottieT812, mentions one: flattening an array, but there's all the other variants of selecting bits of an array:
x=rand(10,10);
flattened=x(:);
Acolumn=x(:,10);
Arow=x(10,:);
y=rand(100);
firstSix=y(1:6);
lastSix=y(end-5:end);
alternate=y(1:2:end);
In order to be able to quickly test a function, I use nargin like so:
function result = multiply(a, b)
if nargin == 0 %no inputs provided, run using defaults for a and b
clc;
disp('RUNNING IN TEST MODE')
a = 1;
b = 2;
end
result = a*b;
Later on, I add a unit test script to test the function for different input conditions.
Using ismember() to merge data organized by text identfiers. Useful when you are analyzing differing periods when entries, in my case company symbols, come and go.
%Merge B into A based on Text identifiers
UniverseA = {'A','B','C','D'};
UniverseB = {'A','C','D'};
DataA = [20 40 60 80];
DataB = [30 50 70];
MergeData = NaN(length(UniverseA),2);
MergeData(:,1) = DataA;
[tf, loc] = ismember(UniverseA, UniverseB);
MergeData(tf,2) = DataB(loc(tf));
MergeData =
20 30
40 NaN
60 50
80 70
Asking 'why' (useful for jarring me out of a Matlab runtime-fail debugging trance at 3am...)
Executing a Simulink model directly from a script (rather than interactively) using the sim command. You can do things like take parameters from a workspace variable, and repeatedly run sim in a loop to simulate something while varying the parameter to see how the behavior changes, and graph the results with whatever graphical commands you like. Much easier than trying to do this interactively, and it gives you much more flexibility than the Simulink "oscilloscope" blocks when visualizing the results. (although you can't use it to see what's going on in realtime while the simulation is running)
A really important thing to know is the DstWorkspace and SrcWorkspace options of the simset command. These control where the "To Workspace" and "From Workspace" blocks get and put their results. Dstworkspace defaults to the current workspace (e.g. if you call sim from inside a function the "To Workspace" blocks will show up as variables accessible from within that same function) but SrcWorkspace defaults to the base workspace and if you want to encapsulate your call to sim you'll want to set SrcWorkspace to current so there is a clean interface to providing/retrieving simulation input parameters and outputs. For example:
function Y=run_my_sim(t,input1,params)
% runs "my_sim.mdl"
% with a From Workspace block referencing I1 as an input signal
% and parameters referenced as fields of the "params" structure
% and output retrieved from a To Workspace block with name O1.
opt = simset('SrcWorkspace','current','DstWorkspace','current');
I1 = struct('time',t,'signals',struct('values',input1,'dimensions',1));
Y = struct;
Y.t = sim('my_sim',t,opt);
Y.output1 = O1.signals.values;
Contour plots with [c,h]=contour and clabel(c,h,'fontsize',fontsize). I usually use the fontsize parameter to reduce the font size so the numbers don't run into each other. This is great for viewing the value of 2-D functions without having to muck around with 3D graphs.
Vectorization:
function iNeedle = findClosest(hay,needle)
%FINDCLOSEST find the indicies of the closest elements in an array.
% Given two vectors [A,B], findClosest will find the indicies of the values
% in vector A closest to the values in vector B.
[hay iOrgHay] = sort(hay(:)'); %#ok must have row vector
% Use histogram to find indices of elements in hay closest to elements in
% needle. The bins are centered on values in hay, with the edges on the
% midpoint between elements.
[iNeedle iNeedle] = histc(needle,[-inf hay+[diff(hay)/2 inf]]); %#ok
% Reversing the sorting.
iNeedle = iOrgHay(iNeedle);
Using persistent (static) variables when running an online algorithm. It may speed up the code in areas like Bayesian machine learning where the model is trained iteratively for the new samples. For example, for computing the independent loglikelihoods, I compute the loglikelihood initially from scratch and update it by summing this previously computed loglikelihood and the additional loglikelihood.
Instead of giving a more specialized machine learning problem, let me give a general online averaging code which I took from here:
function av = runningAverage(x)
% The number of values entered so far - declared persistent.
persistent n;
% The sum of values entered so far - declared persistent.
persistent sumOfX;
if x == 'reset' % Initialise the persistent variables.
n = 0;
sumOfX = 0;
av = 0;
else % A data value has been added.
n = n + 1;
sumOfX = sumOfX + x;
av = sumOfX / n; % Update the running average.
end
Then, the calls will give the following results
runningAverage('reset')
ans = 0
>> runningAverage(5)
ans = 5
>> runningAverage(10)
ans = 7.5000
>> runningAverage(3)
ans = 6
>> runningAverage('reset')
ans = 0
>> runningAverage(8)
ans = 8
I'm surprised that while people mentioned the logical array approach of indexing an array, nobody mentioned the find command.
e.g. if x is an NxMxO array
x(x>20) works by generating an NxMxO logical array and using it to index x (which can be bad if you have large arrays and are looking for a small subset
x(find(x>20)) works by generating list (i.e. 1xwhatever) of indices of x that satisfy x>20, and indexing x by it. "find" should be used more than it is, in my experience.
More what I would call 'tricks'
you can grow/append to arrays and cell arrays if you don't know the size you'll need, by using end + 1 (works with higher dimensions too, so long as the dimensions of the slice match -- so you'll have to initialize x to something other than [] in that case). Not good for numerics but for small dynamic lists of things (or cell arrays), e.g. parsing files.
e.g.
>> x=[1,2,3]
x = 1 2 3
>> x(end+1)=4
x = 1 2 3 4
Another think many people don't know is that for works on any dim 1 array, so to continue the example
>> for n = x;disp(n);end
1
2
3
4
Which means if all you need is the members of x you don't need to index them.
This also works with cell arrays but it's a bit annoying because as it walks them the element is still wrapped in a cell:
>> for el = {1,2,3,4};disp(el);end
[1]
[2]
[3]
[4]
So to get at the elements you have to subscript them
>> for el = {1,2,3,4};disp(el{1});end
1
2
3
4
I can't remember if there is a nicer way around that.
-You can make a Matlab shortcut to an initialization file called startup.m. Here, I define formatting, precision of the output, and plot parameters for my Matlab session (for example, I use a larger plot axis/font size so that .fig's can be seen plainly when I put them in presentations.) See a good blog post from one of the developers about it http://blogs.mathworks.com/loren/2009/03/03/whats-in-your-startupm/ .
-You can load an entire numerical ascii file using the "load" function. This isn't particularly fast, but gets the job done quickly for prototyping (shouldn't that be the Matlab motto?)
-As mentioned, the colon operator and vectorization are lifesavers. Screw loops.
x=repmat([1:10],3,1); % say, x is an example array of data
l=x>=3; % l is a logical vector (1s/0s) to highlight those elements in the array that would meet a certain condition.
N=sum(sum(l));% N is the number of elements that meet that given condition.
cheers -- happy scripting!